S
S
serafims2015-04-23 17:21:54
Delphi
serafims, 2015-04-23 17:21:54

Where does the garbage in a floating point variable come from in Delphi?

There is a text field where I type something like 340.788
Then I take StrToFloat from the text into the field into the single variable, then again FloatToStr - and I get - 340.787994384766.
If you round it up, and then add, say, 100, all the same, garbage is added in the lower digits.

Answer the question

In order to leave comments, you need to log in

6 answer(s)
M
Mercury13, 2015-04-23
@serafims

Because the number 340.788 cannot be represented exactly in floating point. None: neither single, nor double, nor extended. And FloatToStr on standard settings assumes the accuracy of either double or extended.

V
Vladimir Martyanov, 2015-04-23
@vilgeforce

It is taken because of the peculiarities of storing fractional numbers.

V
Vitaly Vitrenko, 2015-04-23
@Vestail

Try Double

A
Archakov Dennis, 2015-04-23
@archakov06

Or use Round()

P
Puma Thailand, 2015-04-23
@opium

Probably all the same, fractions are stored in binary and not decimal still at school, this all goes through

S
serafims, 2015-04-24
@serafims

I solved the problem a little strange - I removed the integer part, multiplied the fractional one to the accuracy I needed (* 1000) after the sign, rounded it up to the integer, removing the fractional part .. I just had to get a number equal to thousandths of the value.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question