[gfortran] setting variable to NaN... - Fortran

This is a discussion on [gfortran] setting variable to NaN... - Fortran ; Hi, I want to set a variable to NaN. This does the trick, with the intel fortran compiler: real :: x x = sqrt(-1.0) print *, x But it does not compile with gfortran: gfortran -Wall foo.f90 In file foo.f90:7 ...

+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 11

[gfortran] setting variable to NaN...

  1. Default [gfortran] setting variable to NaN...


    Hi,

    I want to set a variable to NaN.

    This does the trick, with the intel fortran compiler:

    real :: x

    x = sqrt(-1.0)

    print *, x

    But it does not compile with gfortran:

    gfortran -Wall foo.f90

    In file foo.f90:7

    x = sqrt(-1.0)
    1
    Error: Argument of SQRT at (1) has a negative value

    I guess there is some option to add to the command line, but which one ?

    I have browsed google groups, but unsuccessful.

    All answers are related to test if a variable is a NaN,
    not to set a variable to NaN.

    TIA.

    Cheers,

    --
    http://scipy.org/FredericPetit

  2. Default Re: setting variable to NaN...

    On 25 sep, 08:39, fred <fredantis...@free.fr> wrote:
    > Hi,
    >
    > I want to set a variable to NaN.
    >
    > This does the trick, with the intel fortran compiler:
    >
    > real :: x
    >
    > x = sqrt(-1.0)
    >
    > print *, x
    >
    > But it does not compile with gfortran:
    >
    > gfortran -Wall foo.f90
    >
    > In file foo.f90:7
    >
    > x = sqrt(-1.0)
    > 1
    > Error: Argument of SQRT at (1) has a negative value
    >
    > I guess there is some option to add to the command line, but which one ?
    >
    > I have browsed google groups, but unsuccessful.
    >
    > All answers are related to test if a variable is a NaN,
    > not to set a variable to NaN.
    >
    > TIA.
    >
    > Cheers,
    >
    > --http://scipy.org/FredericPetit


    Hm, one way around it might be:

    real :: arg = -1.0
    real :: x

    x = sqrt(arg)

    That should fool the compiler or even use:

    x = setnan(arg)

    and

    real function setnan(arg)
    setnan = sqrt(arg)
    end function

    You may want to have a look at http://ftp.aset.psu.edu/pub/ger/fortran/hdk/nan.f90
    as well for more information on how NaNs are treated
    by different compilers.

    Regards,

    Arjen


  3. Default Re: setting variable to NaN...

    Arjen Markus <arjen.markus@wldelft.nl> a écrit :

    > real :: arg = -1.0
    > real :: x
    >
    > x = sqrt(arg)

    So obviuous... :-)

    Thanks a lot !

    Cheers,

    --
    http://scipy.org/FredericPetit

  4. Default Re: setting variable to NaN...

    On 25 sep, 09:12, fred <fredantis...@free.fr> wrote:
    > Arjen Markus <arjen.mar...@wldelft.nl> a écrit :
    >
    > > real :: arg = -1.0
    > > real :: x

    >
    > > x = sqrt(arg)

    >
    > So obviuous... :-)
    >
    > Thanks a lot !
    >
    > Cheers,
    >
    > --http://scipy.org/FredericPetit


    Mind you, a very very smart compiler might figure out
    what is going on

    Regards,

    Arjen


  5. Default Re: [gfortran] setting variable to NaN...

    In article <873ax3xn8g.fsf@free.fr>,
    fred <fredantispam@free.fr> writes:
    >
    > Hi,
    >
    > I want to set a variable to NaN.
    >
    > This does the trick, with the intel fortran compiler:
    >
    > real :: x
    >
    > x = sqrt(-1.0)
    >
    > print *, x
    >
    > But it does not compile with gfortran:
    >
    > gfortran -Wall foo.f90
    >
    > In file foo.f90:7
    >
    > x = sqrt(-1.0)
    > 1
    > Error: Argument of SQRT at (1) has a negative value
    >
    > I guess there is some option to add to the command line, but which one ?
    >
    > I have browsed google groups, but unsuccessful.
    >
    > All answers are related to test if a variable is a NaN,
    > not to set a variable to NaN.


    program k
    real nan
    nan = 0.
    nan = nan / nan
    print *, nan
    end program k

    mobile:kargl[203] gfc -o z k.f90
    mobile:kargl[204] ./z
    NaN
    mobile:kargl[205]


    --
    Steve
    http://troutmask.apl.washington.edu/~kargl/

  6. Default Re: setting variable to NaN...

    In article <1190709303.878795.191660@19g2000hsx.googlegroups.com>,
    Arjen Markus <arjen.markus@wldelft.nl> wrote:
    >On 25 sep, 09:12, fred <fredantis...@free.fr> wrote:
    >> Arjen Markus <arjen.mar...@wldelft.nl> a écrit :
    >>
    >> > real :: arg = -1.0
    >> > real :: x

    >>
    >> > x = sqrt(arg)

    >
    >Mind you, a very very smart compiler might figure out
    >what is going on


    So NAG, Compaq and Sun f95 are all very very smart. They all reported
    a floating-point error at run time and core-dumped. I used only the
    default compiler settings in each case.

    -- John Harper, School of Mathematics, Statistics and Computer Science,
    Victoria University, PO Box 600, Wellington 6140, New Zealand
    e-mail john.harper@vuw.ac.nz phone (+64)(4)463 5341 fax (+64)(4)463 5045

  7. Default Re: setting variable to NaN...

    On 25 sep, 23:26, har...@mcs.vuw.ac.nz (John Harper) wrote:
    > In article <1190709303.878795.191...@19g2000hsx.googlegroups.com>,
    > Arjen Markus <arjen.mar...@wldelft.nl> wrote:
    >
    > >On 25 sep, 09:12, fred <fredantis...@free.fr> wrote:
    > >> Arjen Markus <arjen.mar...@wldelft.nl> a écrit :

    >
    > >> > real :: arg = -1.0
    > >> > real :: x

    >
    > >> > x = sqrt(arg)

    >
    > >Mind you, a very very smart compiler might figure out
    > >what is going on

    >
    > So NAG, Compaq and Sun f95 are all very very smart. They all reported
    > a floating-point error at run time and core-dumped. I used only the
    > default compiler settings in each case.
    >
    > -- John Harper, School of Mathematics, Statistics and Computer Science,
    > Victoria University, PO Box 600, Wellington 6140, New Zealand
    > e-mail john.har...@vuw.ac.nz phone (+64)(4)463 5341 fax (+64)(4)463 5045


    Hm, not the type of smartness I was thinking about:
    seeing that the argument to sqrt() would be negative and
    therefore refuse to compile it .

    However, the behaviour does show that it is not that easy
    to get a (quiet?) NaN.

    Regards,

    Arjen


  8. Default Re: setting variable to NaN...

    On Sep 25, 11:35 pm, Arjen Markus <arjen.mar...@wldelft.nl> wrote:
    > On 25 sep, 23:26, har...@mcs.vuw.ac.nz (John Harper) wrote:
    >
    >
    >
    > > In article <1190709303.878795.191...@19g2000hsx.googlegroups.com>,
    > > Arjen Markus <arjen.mar...@wldelft.nl> wrote:

    >
    > > >On 25 sep, 09:12, fred <fredantis...@free.fr> wrote:
    > > >> Arjen Markus <arjen.mar...@wldelft.nl> a écrit :

    >
    > > >> > real :: arg = -1.0
    > > >> > real :: x

    >
    > > >> > x = sqrt(arg)

    >
    > > >Mind you, a very very smart compiler might figure out
    > > >what is going on

    >
    > > So NAG, Compaq and Sun f95 are all very very smart. They all reported
    > > a floating-point error at run time and core-dumped. I used only the
    > > default compiler settings in each case.

    >
    > > -- John Harper, School of Mathematics, Statistics and Computer Science,
    > > Victoria University, PO Box 600, Wellington 6140, New Zealand
    > > e-mail john.har...@vuw.ac.nz phone (+64)(4)463 5341 fax (+64)(4)463 5045

    >
    > Hm, not the type of smartness I was thinking about:
    > seeing that the argument to sqrt() would be negative and
    > therefore refuse to compile it .
    >
    > However, the behaviour does show that it is not that easy
    > to get a (quiet?) NaN.


    If the IEEE modules are available, it should be easy to get
    a quiet NaN.

    Bob Corbett


  9. Default Re: setting variable to NaN...

    robert.corbett@sun.com a écrit :

    > If the IEEE modules are available, it should be easy to get
    > a quiet NaN.

    Tried it with gfortran.
    Unsuccessful.
    Or maybe I did something wrong...

    Cheers,

    --
    http://scipy.org/FredericPetit

  10. Default Re: setting variable to NaN...

    >> If the IEEE modules are available, it should be easy to get
    >> a quiet NaN.

    >
    > Tried it with gfortran.
    > Unsuccessful.
    > Or maybe I did something wrong...


    No, nothing wrong, gfortran doesn't support IEEE modules yet. It should
    be in the next version (4.4).

    --
    FX

+ Reply to Thread
Page 1 of 2 1 2 LastLast

Similar Threads

  1. Setting a varialble using a variable
    By Application Development in forum PHP
    Replies: 8
    Last Post: 11-28-2007, 08:10 AM
  2. Re: Setting LD_LIBRARY_PATH env variable for child process
    By Application Development in forum Perl
    Replies: 0
    Last Post: 04-14-2007, 02:28 AM
  3. setting a server variable?
    By Application Development in forum Inetserver
    Replies: 8
    Last Post: 03-21-2007, 04:01 PM
  4. Setting an instance variable
    By Application Development in forum C
    Replies: 25
    Last Post: 11-25-2004, 08:36 AM