Weird floating point rounding - Bug or how to control it correctly

Jonathan M Davis newsgroup.d at jmdavisprog.com
Thu Sep 14 05:04:08 UTC 2023


On Wednesday, September 13, 2023 9:23:48 PM MDT An Pham via Digitalmars-d-
learn wrote:
> import std.stdio;
>
>      void main()
>      {
>          float f = 6394763.345f;
>
>          import std.format : sformat;
>
>          char[80] vBuffer = void;
>          writeln("6394763.345 = ", sformat(vBuffer[], "%.4f", f));
>
>      }
>
> Output
> 6394763.345 = 6394763.5000

The nature of floating point numbers is such that there a bunch of values
that they can't actually represent, and they get rounded pretty easily
depending on the exact numbers involved. So, in general, you shouldn't
expect floating point numbers to be exact. You will generally do better with
increased precision though, and in this case, it looks like your number will
stay the same if you use double or real instead of float.

I would suggest that you watch this video from dconf 2016 which discusses
floating point values:

https://www.youtube.com/watch?v=YEUAUnamQiA

- Jonathan M Davis





More information about the Digitalmars-d-learn mailing list