Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Changing rounding behavior of WRITE

rawie
Beginner
1,333 Views
How canI change the rounding behavior of the WRITE-statement?
I tried the following:
write(*,'(F9.0)') 110.5
write(*,'(F9.0)') 111.5
write(*,'(F9.0)') 112.5
write(*,'(F9.0)') 113,5
and got (round to evenI suppose):
110.
112.
112.
114.
but i want 'normal' rounding for all writes in my program so that the result should looks like this:
111.
112.
113.
114.
Using SETCONTROLFPQQdoesn't work, because it' no problem auf rounding the last bit of an real.
I'm using still CVF 6.1.A.
0 Kudos
7 Replies
Intel_C_Intel
Employee
1,333 Views
Well, that is the mathematically correct way to round..however, if you want to round up to integer, just add.5
Think about how you want to round negative numbers, though.
0 Kudos
rawie
Beginner
1,333 Views
Even if it's the mathematically correct I think it's unusual. Microsoft Visual FORTRAN didn't round to even.Printf in C doesn't too. It's the first time I ever heard of 'round to even'.
But this discussion doesn't solve my problem. Adding 0.5 doesn't too because I've lots of writes in my program with lots of different formats (sometimes variable formats) and rounding has to be 'correct' forevery positionafter the comma. I'm looking for a solution witch changes the behavior of all writes.
0 Kudos
Steven_L_Intel1
Employee
1,333 Views
This is the IEEE Floating Point Standard specified behavior and is correct. If a value to be rounded is exactly half way between two representable values, it is rounded such that the low-order bit is zero ("round to even"). This eliminates a bias inherent in "round away from zero".
The Fortran standard does not currently specify how this rounding is to be done.
0 Kudos
rawie
Beginner
1,333 Views
I think that's not right. Look at these samples:
write(*,'(F9.1)') 0.45
write(*,'(F9.1)') 0.55
FORTRAN writes 0.4 and 0.6 to the screen. The binary representations of 0.4 and 0.6 are 3ECCCCCD and3F19999A. But the binary representation of 0.5 is3F000000 so 0.5 has to bethe rightresult for both roundings!?
This shows that the write-statement rounds to even but to an even digit and not to an even bit! I think it has nothing to do with IEEE. There are simply two methods to round if the last digit is 5 and no digits follow.
Even NINT and ANINT in CVF don't use round to even! As mentioned before printf doesn't and Microsoft Visual FORTRAN doesn't too. Why does the write-statement ofCVF?
Is there really no method to change this behavior?
0 Kudos
Steven_L_Intel1
Employee
1,333 Views
You're not approaching it the right way. The rounding is done in decimal so the "round to even" is applied to the decimal digits.
There was no Microsoft Visual Fortran. Microsoft Fortran PowerStation indeed did not round this way, as did early versions of Digital Visual Fortran. It was a bug that we fixed in Digital Visual Fortran. There is no control over this.
NINT and ANINT have explicit language in the standard for how they should round. I can't speak to C's printf.
See this article for further details.
0 Kudos
Steven_L_Intel1
Employee
1,333 Views
I will add that Fortran 2003 has a feature that allows a user to specify the type of rounding to do in this case. So at some point in the future, you would have that control.
0 Kudos
garyscott
Beginner
1,333 Views
I expect that most of us naive types will expect the "nint" behavior since that's what we were taught in school. Probably should have been a compiler switch to implement this type of change.
0 Kudos
Reply