Answer the question
In order to leave comments, you need to log in
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
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.
It is taken because of the peculiarities of storing fractional numbers.
Probably all the same, fractions are stored in binary and not decimal still at school, this all goes through
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 questionAsk a Question
731 491 924 answers to any question