| Register | FAQ | Calendar | Search | Today's Posts | Mark Forums Read |
|
#1
| |||
| |||
| 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 |
|
#2
| |||
| |||
| 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." |
|
#3
| |||
| |||
| "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 |
|
#4
| |||
| |||
| 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 |
|
#5
| |||
| |||
| 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 |
|
#6
| |||
| |||
| 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 |
|
#7
| |||
| |||
| 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. |
|
#8
| |||
| |||
| 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 |
|
#9
| |||
| |||
| 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 |
|
#10
| |||
| |||
| "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 |
![]() |
| Thread Tools | |
| Display Modes | |
In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.