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.
連結已複製
7 回應
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.
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.
I think that's not right. Look at these samples:
write(*,'(F9.1)') 0.45
write(*,'(F9.1)') 0.55FORTRAN 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?
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.
