Fixing Floating Point Error

Posted by HannesNZ on Stack Overflow See other posts from Stack Overflow or by HannesNZ
Published on 2010-05-30T23:19:27Z Indexed on 2010/05/30 23:22 UTC
Read the original article Hit count: 245

Filed under:
|

I have some code that gets the leading value (non-zero) of a Double using normal math instead of String Math...

For Example:
0.020 would return 2
3.12 would return 3
1000 should return 1

The code I have at the moment is:

LeadingValue := Trunc(ResultValue * Power(10, -(Floor(Log10(ResultValue)))))

However when ResultValue is 1000 then LeadingValue ends up as 0.

What can I do to fix this problem I'm assuming is being caused by floating point errors?

Thanks.

© Stack Overflow or respective owner

Related posts about delphi

Related posts about floating-accuracy