float and double question

Discussion in 'C++' started by Pat, May 21, 2004.

  1. Pat

    Pat Guest

    Give two double-typed variable X and Y.

    If (X==Y) is true,

    then how about the following results:
    (float(X) > float(Y))?
    (float(X) < float(Y))?
    (float(X) >= float(Y))?
    ( X > float(Y) )?
    ( X < float(Y) )?
    ( X == float(Y) )?

    Will the results be independent of compliers and OS platforms?

    Thanks. Pat
     
    Pat, May 21, 2004
    #1
    1. Advertising

  2. "Pat" <> wrote...
    > Give two double-typed variable X and Y.
    >
    > If (X==Y) is true,
    >
    > then how about the following results:
    > (float(X) > float(Y))?
    > (float(X) < float(Y))?
    > (float(X) >= float(Y))?
    > ( X > float(Y) )?
    > ( X < float(Y) )?
    > ( X == float(Y) )?
    >
    > Will the results be independent of compliers and OS platforms?


    Unless there is an overflow (resulting in infinity values), the
    expression

    float(X) == float(Y)

    should always evaluate 'true' if X == Y evaluates 'true'.

    Also, if you attempt to compare float(X) and Y [or X and float(Y)],
    both will be converted back to 'double' and the result will most
    definitely depend on values of X and Y, compiler, and hardware.

    Victor
     
    Victor Bazarov, May 21, 2004
    #2
    1. Advertising

  3. Pat,

    I did some experiments, I found that float(X) is rounding on my MVC++
    compiler, so if X=0.499999999, float(X) = 0.5, but if X = 0.511111111,
    float(X) = 0.51111, so we get both > and < situations.

    If X == Y, then always float(X) == float(Y)


    dave


    "Pat" <> wrote in message news:40ad7e91$-cable.com...
    > Give two double-typed variable X and Y.
    >
    > If (X==Y) is true,
    >
    > then how about the following results:
    > (float(X) > float(Y))?
    > (float(X) < float(Y))?
    > (float(X) >= float(Y))?
    > ( X > float(Y) )?
    > ( X < float(Y) )?
    > ( X == float(Y) )?
    >
    > Will the results be independent of compliers and OS platforms?
    >
    > Thanks. Pat
    >
    >
     
    Dave Townsend, May 21, 2004
    #3
  4. Pat

    Ian Guest

    Dave Townsend wrote:
    > Pat,
    >
    > I did some experiments, I found that float(X) is rounding on my MVC++
    > compiler, so if X=0.499999999, float(X) = 0.5, but if X = 0.511111111,
    > float(X) = 0.51111, so we get both > and < situations.
    >

    Think about this for a moment; abs(0.5-499999999) !=
    abs(0.5-0.511111111). 7 orders of magnitude difference.

    Ian

    > If X == Y, then always float(X) == float(Y)
    >
    >
    > dave
    >
    >
    > "Pat" <> wrote in message news:40ad7e91$-cable.com...
    >
    >>Give two double-typed variable X and Y.
    >>
    >>If (X==Y) is true,
    >>
    >>then how about the following results:
    >>(float(X) > float(Y))?
    >>(float(X) < float(Y))?
    >>(float(X) >= float(Y))?
    >>( X > float(Y) )?
    >>( X < float(Y) )?
    >>( X == float(Y) )?
    >>
    >>Will the results be independent of compliers and OS platforms?
    >>
    >>Thanks. Pat
    >>
    >>

    >
    >
    >
     
    Ian, May 21, 2004
    #4
  5. Ian,

    Sorry, not following what you said.

    I'm saying that because the float type casting operation rounds, in some
    cases
    it will round down and some it will round up, so we get both < and > for the
    examples.

    dave

    "Ian" <> wrote in message
    news:...
    > Dave Townsend wrote:
    > > Pat,
    > >
    > > I did some experiments, I found that float(X) is rounding on my MVC++
    > > compiler, so if X=0.499999999, float(X) = 0.5, but if X = 0.511111111,
    > > float(X) = 0.51111, so we get both > and < situations.
    > >

    > Think about this for a moment; abs(0.5-499999999) !=
    > abs(0.5-0.511111111). 7 orders of magnitude difference.
    >
    > Ian
    >
    > > If X == Y, then always float(X) == float(Y)
    > >
    > >
    > > dave
    > >
    > >
    > > "Pat" <> wrote in message

    news:40ad7e91$-cable.com...
    > >
    > >>Give two double-typed variable X and Y.
    > >>
    > >>If (X==Y) is true,
    > >>
    > >>then how about the following results:
    > >>(float(X) > float(Y))?
    > >>(float(X) < float(Y))?
    > >>(float(X) >= float(Y))?
    > >>( X > float(Y) )?
    > >>( X < float(Y) )?
    > >>( X == float(Y) )?
    > >>
    > >>Will the results be independent of compliers and OS platforms?
    > >>
    > >>Thanks. Pat
    > >>
    > >>

    > >
    > >
    > >
     
    Dave Townsend, May 21, 2004
    #5
  6. Pat

    David Logan Guest

    Pat wrote:
    > Give two double-typed variable X and Y.
    >
    > If (X==Y) is true,
    >
    > then how about the following results:
    > (float(X) > float(Y))?
    > (float(X) < float(Y))?
    > (float(X) >= float(Y))?
    > ( X > float(Y) )?
    > ( X < float(Y) )?
    > ( X == float(Y) )?
    >
    > Will the results be independent of compliers and OS platforms?
    >
    > Thanks. Pat
    >
    >


    No, they will not be independent of compilers, nor of OS platforms.
    Floating point numbers, the underlying machine instructions and
    registers that manage them are different. You will get subtle
    differences, especially as you increase in precision. We have trouble on
    a regular basis with floating point numbers in our software, compounded
    when we port to different platforms.

    Floating point numbers are good to use in some situations, but you
    REALLY want to avoid comparing them whenever possible.

    David Logan
     
    David Logan, May 21, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JKop
    Replies:
    4
    Views:
    16,544
  2. Sydex
    Replies:
    12
    Views:
    6,568
    Victor Bazarov
    Feb 17, 2005
  3. bd
    Replies:
    0
    Views:
    652
  4. Alexander Malkis
    Replies:
    10
    Views:
    915
  5. Carsten Fuchs
    Replies:
    45
    Views:
    1,612
    James Kanze
    Oct 8, 2009
Loading...

Share This Page