- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
Link Copied
7 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page