internal write rounds wrong

This is a discussion on internal write rounds wrong within the Fortran forums in Programming Languages category; Hi, i want tol display a real number with less decimal places. What i do is an internal write with a format Fx.x like "write(char,form) number" Normally it works fine but my Fortran rounds the last 5 down. Example: 1.555 is rounded by format F4.2 to 1.55 not to 1.56 !!! Has somebody an idea to solve this problem? Thanks a lot...

Go Back   Application Development Forum > Programming Languages > Fortran

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 09-04-2008, 09:11 AM
phantasmus
Guest
 
Default internal write rounds wrong

Hi,
i want tol display a real number with less decimal places.
What i do is an internal write with a format Fx.x like
"write(char,form) number"

Normally it works fine but my Fortran rounds the last 5 down.
Example: 1.555 is rounded by format F4.2 to 1.55 not to 1.56 !!!

Has somebody an idea to solve this problem?

Thanks a lot



Reply With Quote
  #2  
Old 09-04-2008, 09:50 AM
Tim Prince
Guest
 
Default Re: internal write rounds wrong

phantasmus wrote:
> Hi,
> i want tol display a real number with less decimal places.
> What i do is an internal write with a format Fx.x like
> "write(char,form) number"
>
> Normally it works fine but my Fortran rounds the last 5 down.
> Example: 1.555 is rounded by format F4.2 to 1.55 not to 1.56 !!!
>
> Has somebody an idea to solve this problem?
>

Unless you have a Fortran which uses decimal data formats, you can't have
a value of exactly 1.555. Supposing that the value is 1.55499, and you
still want the behavior you describe, you must write in your own code to
accommodate your style of rounding.
Supposing that you did have a value exactly half way between 2 values
represented by format F4.2, I suspect that it is OK for a run time library
to follow IEEE style rounding "to nearest even."
Reply With Quote
  #3  
Old 09-04-2008, 10:16 AM
Kurt Kallblad
Guest
 
Default Re: internal write rounds wrong


"phantasmus" <kramer-fahrer@arcor.de> wrote in message
news:8254b0fd-bdd6-47b8-b975-e4b4d822f0f2@v16g2000prc.googlegroups.com...
> Hi,
> i want tol display a real number with less decimal places.
> What i do is an internal write with a format Fx.x like
> "write(char,form) number"
>
> Normally it works fine but my Fortran rounds the last 5 down.
> Example: 1.555 is rounded by format F4.2 to 1.55 not to 1.56
> !!!
>
> Has somebody an idea to solve this problem?
>
> Thanks a lot
>


The problem is that 1.555 does not have an exact representation
in the computer.
Start to read about floating point numbers, a search on google
will give a lot to read.

Kurt

Reply With Quote
  #4  
Old 09-04-2008, 10:28 AM
Herman D. Knoble
Guest
 
Default Re: internal write rounds wrong


On Thu, 4 Sep 2008 06:11:04 -0700 (PDT), phantasmus <kramer-fahrer@arcor.de> wrote:
-|Hi,
-|i want tol display a real number with less decimal places.
-|What i do is an internal write with a format Fx.x like
-|"write(char,form) number"
-|
-|Normally it works fine but my Fortran rounds the last 5 down.
-|Example: 1.555 is rounded by format F4.2 to 1.55 not to 1.56 !!!
-|
-|Has somebody an idea to solve this problem?
-|
-|Thanks a lot
-|
For what it's worth, an example (using g95):

function round(x) result(r)
real, intent(in) :: x
real :: r
r=floor(x+0.5)
end function round

Real :: x
x=1.555
write(*,"(1x,f4.2,2x,f4.2)") x, 1.E-3*Round(x*1.E3)
end

results displayed: 1.55 1.56

Skip Knoble
Reply With Quote
  #5  
Old 09-04-2008, 10:50 AM
Michel Olagnon
Guest
 
Default Re: internal write rounds wrong

Herman D. Knoble wrote:
> On Thu, 4 Sep 2008 06:11:04 -0700 (PDT), phantasmus <kramer-fahrer@arcor.de> wrote:
> -|Hi,
> -|i want tol display a real number with less decimal places.
> -|What i do is an internal write with a format Fx.x like
> -|"write(char,form) number"
> -|
> -|Normally it works fine but my Fortran rounds the last 5 down.
> -|Example: 1.555 is rounded by format F4.2 to 1.55 not to 1.56 !!!
> -|
> -|Has somebody an idea to solve this problem?
> -|
> -|Thanks a lot
> -|
> For what it's worth, an example (using g95):
>
> function round(x) result(r)
> real, intent(in) :: x
> real :: r
> r=floor(x+0.5)
> end function round


Why not use the NINT intrinsic ?

>
> Real :: x
> x=1.555
> write(*,"(1x,f4.2,2x,f4.2)") x, 1.E-3*Round(x*1.E3)
> end
>
> results displayed: 1.55 1.56
>
> Skip Knoble



Reply With Quote
  #6  
Old 09-04-2008, 11:18 AM
Herman D. Knoble
Guest
 
Default Re: internal write rounds wrong

On Thu, 04 Sep 2008 16:50:26 +0200, Michel Olagnon <molagnon@ifremer-a-oter.fr> wrote:

-|Herman D. Knoble wrote:
-|> On Thu, 4 Sep 2008 06:11:04 -0700 (PDT), phantasmus <kramer-fahrer@arcor.de> wrote:
-|> -|Hi,
-|> -|i want tol display a real number with less decimal places.
-|> -|What i do is an internal write with a format Fx.x like
-|> -|"write(char,form) number"
-|> -|
-|> -|Normally it works fine but my Fortran rounds the last 5 down.
-|> -|Example: 1.555 is rounded by format F4.2 to 1.55 not to 1.56 !!!
-|> -|
-|> -|Has somebody an idea to solve this problem?
-|> -|
-|> -|Thanks a lot
-|> -|
-|> For what it's worth, an example (using g95):
-|>
-|> function round(x) result(r)
-|> real, intent(in) :: x
-|> real :: r
-|> r=floor(x+0.5)
-|> end function round
-|
-|Why not use the NINT intrinsic ?
-|
Michel: Excellent idea.

implicit none
Real :: x
x=1.555
write(*,"(1x,f4.2,2x,f4.2)") x, 1.E-3*NINT(x*1.E3)
end

-|
-|> results displayed: 1.55 1.56


Reply With Quote
  #7  
Old 09-04-2008, 04:38 PM
Leslie Ballentine
Guest
 
Default Re: internal write rounds wrong

phantasmus <kramer-fahrer@arcor.de> wrote:
: Hi,
: i want tol display a real number with less decimal places.
: What i do is an internal write with a format Fx.x like
: "write(char,form) number"

: Normally it works fine but my Fortran rounds the last 5 down.
: Example: 1.555 is rounded by format F4.2 to 1.55 not to 1.56 !!!

: Has somebody an idea to solve this problem?

: Thanks a lot

Why should this be regarded as a problem?
The roundoff error is the same, whether rounded up or down.
I know that there is an oldfashioned convention to do it the way
that you want, but that convention is as arbitrary and unfuctional
as the grammatical "rule" against splitting an infinitive.

Reply With Quote
  #8  
Old 09-04-2008, 04:54 PM
Richard Maine
Guest
 
Default Re: internal write rounds wrong

Leslie Ballentine <ballenti@sfu.ca> wrote:

> phantasmus <kramer-fahrer@arcor.de> wrote:


> : Normally it works fine but my Fortran rounds the last 5 down.
> : Example: 1.555 is rounded by format F4.2 to 1.55 not to 1.56 !!!

....
> Why should this be regarded as a problem?
> The roundoff error is the same, whether rounded up or down.


Perhaps in some other cases, but in this particular case, that's not so.
This is because, as other posters have noted, there is no such
representable number as 1.555 in the floatting point formats in use. The
actual number will be either slightly larger or slightly smaller than
that. If the question were about rounding something like, say 1.5, then
things would be as you say.

I'll note that f2003 has some rounding control options, but they aren't
going to change the far more fundamental problem that there is no such
representable number as exactly 1.555.

I am also quite puzzled as to why the OP identifies this as having
anything to do with internal writes in particular. Is he claiming that
internal writes are different from external ones? If so, that would
indeed be strange. If not, the subject line seems misleading.

--
Richard Maine | Good judgement comes from experience;
email: last name at domain . net | experience comes from bad judgement.
domain: summertriangle | -- Mark Twain
Reply With Quote
  #9  
Old 09-04-2008, 08:44 PM
glen herrmannsfeldt
Guest
 
Default Re: internal write rounds wrong

Tim Prince wrote:
(snip)

> Unless you have a Fortran which uses decimal data formats, you can't
> have a value of exactly 1.555.


Maybe not so much longer. I believe IBM is now selling such
machines, though I don't know if the IEEE standard is final yet.

> Supposing that the value is 1.55499, and
> you still want the behavior you describe, you must write in your own
> code to accommodate your style of rounding.
> Supposing that you did have a value exactly half way between 2 values
> represented by format F4.2, I suspect that it is OK for a run time
> library to follow IEEE style rounding "to nearest even."


In a real calculation, in decimal or any other base, you will
very likely have a result slightly more or slightly less than
exactly 1.555, and it will round in the expected way.

In 32 bit decimal, you would have 1 followed by seven decimal
digits, so a one in 10000 chance of coming out 1.5550000
in most calculations.

-- glen

Reply With Quote
  #10  
Old 09-05-2008, 04:12 AM
Kurt Kallblad
Guest
 
Default Re: internal write rounds wrong


"Leslie Ballentine" <ballenti@sfu.ca> wrote in message
news:g9ph0v$8oq$1@morgoth.sfu.ca...
> phantasmus <kramer-fahrer@arcor.de> wrote:
> : Hi,
> : i want tol display a real number with less decimal places.
> : What i do is an internal write with a format Fx.x like
> : "write(char,form) number"
>
> : Normally it works fine but my Fortran rounds the last 5 down.
> : Example: 1.555 is rounded by format F4.2 to 1.55 not to 1.56
> !!!
>
> : Has somebody an idea to solve this problem?
>
> : Thanks a lot
>
> Why should this be regarded as a problem?
> The roundoff error is the same, whether rounded up or down.
> I know that there is an oldfashioned convention to do it the
> way
> that you want, but that convention is as arbitrary and
> unfuctional
> as the grammatical "rule" against splitting an infinitive.
>


Leaving the problems with computer's floating numbers:

The rounding metod is not totally arbitrary. To round up in all
cases can be a problem, e.g. in statistics if a lot of numbers
end exactly halfway.

In my old textbook in numerical analys the rounding rule was to
round to an even number if the number ends exactly halfway. E.g.
1.555 -> 1.56 and 1.545 -> 1.54 but 1.5451 -> 1.55.

Kurt

Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 02:24 PM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.