Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

format problem

Status
Not open for further replies.

sagn

Programmer
Jun 7, 2001
166
US
I have a a string with value '7.0445'.
It is placed in a real variable (Ci) and appears as

7.044500 in the debugger.

When I try and write it to a string (STR) using F8.3
WRITE(STR,FMT='(F8.3)')Ci

It comes out as 7.044 (NOT 7.045)

However if I make the variable = to 7.044510
Then the value placed in the string is '7.045'

Why could this be?
Why, in the first instance, is the REAL not being rounded?

thanks
 
Looks like the rounding is done correctly!
The accountants and scientists do not like to round up automatically at 5, because it will SYSTEMATICALLY gives a number too big on the average. So if the remainder to be rounded is exactly 0.5 unit, it rounds to the next even digit.

To test it on your compiler, try displaying
7.0445 and 7.0455
If the above scheme is used, it should give 7.044 and 7.046 respectively.
However, 7.044501 and 7.045501 will give 7.045 and 7.046 respectively, as they are over 0.5 unit and the above rule no longer applies.
 
Some addition: think about floating point arithmetics. You have not exactly 7.0445 or 7.044510 or what else with decimal fractions on binary floats of modern computers...
Comp floats are not math reals...
 
Thanks. I actually was thinking it had something to do with the floating point arithmetic...

But was not sure why that would be.

Anyway, thanks for the insight.

diane
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top