For Programmers: Free Programming Magazines  


Home > Archive > Fortran > August 2007 > Why zero is represented by an extremely small value?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Why zero is represented by an extremely small value?
www

2007-07-24, 7:09 pm

Hi,

I don't know a lot about Fortran. I noticed that when a variable value
is 0, if I print it out, it is a very small negative value, like
-7.6293945E-06. Is this normal in Fortran?

I noticed this when I was debugging a Java program which was translated
from Fortran one line by one line

<Fortran>

IF (TA .LT. 0.) THEN
...
</Fortran>

<Java>

if (TA <= 0.0) //It cannot be "<", must be "<=", otherwise the Java
results is different from Fortran results
....
</Java>

Thank you very much.
Louis Krupp

2007-07-24, 7:09 pm

www wrote:
> Hi,
>
> I don't know a lot about Fortran. I noticed that when a variable value
> is 0, if I print it out, it is a very small negative value, like
> -7.6293945E-06. Is this normal in Fortran?
>
> I noticed this when I was debugging a Java program which was translated
> from Fortran one line by one line
>
> <Fortran>
>
> IF (TA .LT. 0.) THEN
> ..
> </Fortran>
>
> <Java>
>
> if (TA <= 0.0) //It cannot be "<", must be "<=", otherwise the Java
> results is different from Fortran results
> ...
> </Java>
>
> Thank you very much.


Zero is represented as zero. Just because a Java variable "is 0"
doesn't mean the corresponding Fortran variable is also zero; there are
lots of things that happen with floating point arithmetic.

Can you reproduce this with small Fortran and Java programs and post those?

Louis
Richard Maine

2007-07-24, 7:09 pm

www <www@nospam.com> wrote:

> I don't know a lot about Fortran. I noticed that when a variable value
> is 0, if I print it out, it is a very small negative value, like
> -7.6293945E-06. Is this normal in Fortran?


Not only is it not normal, it doesn't happen. Ever. Period. If the
variable printed out like that, then its value was not zero.

You talk about this being a translation of a Java program where the
value in the Java program was zero. You appear to be assuming that, of
course, the translation was perfect and gets identical results, so that
any difference must relate to the printing. You are looking in the wrong
place. I would suggest instead looking at the fidelity of the
translation.

--
Richard Maine | Good judgement comes from experience;
email: last name at domain . net | experience comes from bad judgement.
domain: summertriangle | -- Mark Twain
glen herrmannsfeldt

2007-07-24, 7:09 pm

www wrote:

> I don't know a lot about Fortran. I noticed that when a variable value
> is 0, if I print it out, it is a very small negative value, like
> -7.6293945E-06. Is this normal in Fortran?


No, in that case it isn't zero.

It is, though, very easy to get small but non-zero numbers using
floating point arithmetic when the exact algebraic value would
be an exact zero.

-- glen

Wade Ward

2007-07-25, 7:08 pm


"glen herrmannsfeldt" <gah@ugcs.caltech.edu> wrote in message
news:Er2dnf9BS9qN9zvbnZ2dnUVZ_sbinZ2d@co
mcast.com...
> www wrote:
>
>
> No, in that case it isn't zero.
>
> It is, though, very easy to get small but non-zero numbers using
> floating point arithmetic when the exact algebraic value would
> be an exact zero.

I'm a little surprised that you and Richard are so emphatic, when the
residual is several parts in 10^6. If you zeroed out the bits of a single
precision number and then printed it as a double, what would be the largest
number you would expect? Read "large" as magnitude; my head's going to
explode if I read any more about the two sides of zero.
--
Wade Ward


Rich Townsend

2007-07-25, 7:08 pm

Wade Ward wrote:
> "glen herrmannsfeldt" <gah@ugcs.caltech.edu> wrote in message
> news:Er2dnf9BS9qN9zvbnZ2dnUVZ_sbinZ2d@co
mcast.com...
> I'm a little surprised that you and Richard are so emphatic, when the
> residual is several parts in 10^6. If you zeroed out the bits of a single
> precision number and then printed it as a double, what would be the largest
> number you would expect?


Zero. Once a number is identically equal to zero, it should remain as zero
through any promotion/demotion to another precision.

Read "large" as magnitude; my head's going to
> explode if I read any more about the two sides of zero.

Wade Ward

2007-07-25, 7:08 pm


"Rich Townsend" <rhdt@barVOIDtol.udel.edu> wrote in message
news:f88gij$7af$1@scrotar.nss.udel.edu...
> Wade Ward wrote:
[color=darkred]
>
> Zero. Once a number is identically equal to zero, it should remain as zero
> through any promotion/demotion to another precision.

I guess that's the point. I think a person could cook up ways to get around
that, but why would you want to?
--
WW


glen herrmannsfeldt

2007-07-25, 7:08 pm

Wade Ward wrote:

(snip)

> I'm a little surprised that you and Richard are so emphatic, when the
> residual is several parts in 10^6. If you zeroed out the bits of a single
> precision number and then printed it as a double, what would be the largest
> number you would expect? Read "large" as magnitude; my head's going to
> explode if I read any more about the two sides of zero.


I am not so sure what you are asking. If you have two floating point
single precision values that are as close as they can be without being
equal, the difference will be about 1e-7 times either value. If the
actual value was, for example, 1e37 the difference could be 1e30.
If the values were around 10, the difference would be around 1e-6.

-- glen

Richard Maine

2007-07-25, 7:08 pm

Wade Ward <zaxfuuq@invalid.net> wrote:

> "glen herrmannsfeldt" <gah@ugcs.caltech.edu> wrote in message
> news:Er2dnf9BS9qN9zvbnZ2dnUVZ_sbinZ2d@co
mcast.com...
[color=darkred]
> I'm a little surprised that you and Richard are so emphatic, when the
> residual is several parts in 10^6. If you zeroed out the bits of a single
> precision number and then printed it as a double, what would be the largest
> number you would expect? Read "large" as magnitude; my head's going to
> explode if I read any more about the two sides of zero.


There is no such thing as "printing it as a double" so the question is
moot. I'm not sure what you mean, but my guess would be one of

1. Using a D format. That doesn't "print it as a double". All that does
is use a D as the exponent letter. It is also a bit useless any more,
but that's another matter. Perhaps you think this actually does
something more than that. It doesn't. Thus this will also print 0.

2. Using a format with lots of digits. They will all be zero, no matter
how many there are. I guarantee you that you won't find a compiler where
this isn't the case. Would you like to place money on it? Large sums?
(This is known as a sucker bet).

3. Converting the number to double and then printing. This will also
print zero. See above offer.

4. Illegal tricks with equivalence or other simillar things. Your
discussion of the bits makes it seem as though this might be what you
have in mind. In that case, first it isn't legal Fortran at all, and
second, to the extent that it does anything, if it prints the double as
anything other than zero, then the double was not zero. The fact that
the double might have some zero bits is quite irrelevant. It still isn't
zero. A number isn't zero just because it has a zero bit in it somewhere
- or even a lot of zro bits.

5. If you don't mean any f the above, then I haven't a clue what you
would mean, but I'm sure it is not relevant to the OP.

I am so emphatic because it is so true. See above guarantees. You can
quibble all you want about legalistic outs in the standard. I can play
devil's advocate too. (Claim that the program exceeds the capacity of
the compiler; the standard allows that claim; thus it isn't covered by
the standard and anything that the compiler wants to do is ok; Q.E.D.)
But that is nothing other than a game. You will *NOT* find any compilers
that get this one wrong. The OP needs the emphasis to point him in the
right direction. Anything that encouraged him in his mistaken notion is
a disservice.

--
Richard Maine | Good judgement comes from experience;
email: last name at domain . net | experience comes from bad judgement.
domain: summertriangle | -- Mark Twain
Wade Ward

2007-07-25, 7:08 pm


"Richard Maine" <nospam@see.signature> wrote in message
news:1i1tdh7.f4vnmp1bd1dwqN%nospam@see.signature...
> Wade Ward <zaxfuuq@invalid.net> wrote:
>


[color=darkred]
> I am so emphatic because it is so true. See above guarantees. You can
> quibble all you want about legalistic outs in the standard. I can play
> devil's advocate too. (Claim that the program exceeds the capacity of
> the compiler; the standard allows that claim; thus it isn't covered by
> the standard and anything that the compiler wants to do is ok; Q.E.D.)
> But that is nothing other than a game. You will *NOT* find any compilers
> that get this one wrong. The OP needs the emphasis to point him in the
> right direction. Anything that encouraged him in his mistaken notion is
> a disservice.

It sounds like fortran is pretty air-tight on this one. *Compilers* won't
make this mistake (I don't think you've ever offered me a wager). Humans
can, as in the original post, unless OP is disingenuous, in particular when
they're crossing syntax borders.

In general, I don't like to consider cases of intentional pathology in a
language, like writing bytes to disc with one program, and reading them in
pairs with a different one. There's too much legal stuff to learn instead.
--
Wade Ward


Paul van Delst

2007-07-26, 7:07 pm

glen herrmannsfeldt wrote:[color=darkred]
> Wade Ward wrote:
>
> (snip)
>

Goodness, it seems working with floating point numbers is akin to working with power
tools. Even reading the operating instructions[*] (one hopes) and wearing protective
equipment doesn't prevent some folks from sawing their fingers off.

:o)

cheers,

paulv

[*] e.g. http://docs.sun.com/app/docs/doc/800-7895/6hos0aou4

--
Paul van Delst Ride lots.
CIMSS @ NOAA/NCEP/EMC Eddy Merckx
robin

2007-08-29, 7:08 pm

"www" <www@nospam.com> wrote in message news:f85gku$e1j$1@news.nems.noaa.gov...
> Hi,
>
> I don't know a lot about Fortran. I noticed that when a variable value
> is 0,


If the value is zero, it will print as zero.

> if I print it out, it is a very small negative value, like
> -7.6293945E-06.


It prints this value because the value of the variable is
actually -7.6293945E-06.

> Is this normal in Fortran?
>
> I noticed this when I was debugging a Java program which was translated
> from Fortran one line by one line
>
> <Fortran>
>
> IF (TA .LT. 0.) THEN
> ..
> </Fortran>
>
> <Java>
>
> if (TA <= 0.0) //It cannot be "<", must be "<=", otherwise the Java
> results is different from Fortran results
> ...
> </Java>


This is insufficient code on which to base any explanation.



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com