Software Archive
Read-only legacy content
17061 Discussions

simple rounding question

ksweeney
Beginner
368 Views
How does Compaq Fortran version 6.1 handle rounding (actually printing) of real numbers? Specifically, I have an expression which is evaluated to 0.65. When I print this number to a file using F3.1 formatting it prints as 0.6. It appears that the .05 is rounded down. Is there an easy way to ensure that in all such cases it is rounded to 0.7?
Thanks for any help.

Kevin
0 Kudos
1 Reply
Steven_L_Intel1
Employee
368 Views
0.65 is not exactly representable - the actual internal value is approximately 0.649999976, which rounds down to 0.6. 0.7 would be incorrect.

I will note that if the value WERE exactly 0.65, then you'd still get 0.6 because IEEE rounding rules would be followed - a value exactly halfway between two representable values is rounded so that the low-order digit is even. (On VMS, VAX rounding rules would be followed if the VAX floating types were being used.) As another example, an IEEE 2.5 rounds to 2. in F6.0 format!

Note that the Fortran standard does not currently specify what "rounding" means, except in the particular case of the NINT intrinsic.

Our latest newsletter, sent to registered users, has an appropriate article on floating point that addresses some of these issues.

Steve
0 Kudos
Reply