Connecting Tech Pros Worldwide Help | Site Map

float and double question

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 22nd, 2005, 11:23 AM
Pat
Guest
 
Posts: n/a
Default float and double question

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



  #2  
Old July 22nd, 2005, 11:23 AM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: float and double question

"Pat" <Pat@Pat.com> wrote...[color=blue]
> 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?[/color]

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


  #3  
Old July 22nd, 2005, 11:23 AM
Dave Townsend
Guest
 
Posts: n/a
Default Re: float and double question

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" <Pat@Pat.com> wrote in message news:40ad7e91$1_1@rain.i-cable.com...[color=blue]
> 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
>
>[/color]


  #4  
Old July 22nd, 2005, 11:23 AM
Ian
Guest
 
Posts: n/a
Default Re: float and double question

Dave Townsend wrote:[color=blue]
> 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.
>[/color]
Think about this for a moment; abs(0.5-499999999) !=
abs(0.5-0.511111111). 7 orders of magnitude difference.

Ian
[color=blue]
> If X == Y, then always float(X) == float(Y)
>
>
> dave
>
>
> "Pat" <Pat@Pat.com> wrote in message news:40ad7e91$1_1@rain.i-cable.com...
>[color=green]
>>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
>>
>>[/color]
>
>
>[/color]
  #5  
Old July 22nd, 2005, 11:23 AM
Dave Townsend
Guest
 
Posts: n/a
Default Re: float and double question


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" <noone@nowhere.com> wrote in message
news:1085129449.933154@drone5.qsi.net.nz...[color=blue]
> Dave Townsend wrote:[color=green]
> > 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.
> >[/color]
> Think about this for a moment; abs(0.5-499999999) !=
> abs(0.5-0.511111111). 7 orders of magnitude difference.
>
> Ian
>[color=green]
> > If X == Y, then always float(X) == float(Y)
> >
> >
> > dave
> >
> >
> > "Pat" <Pat@Pat.com> wrote in message[/color][/color]
news:40ad7e91$1_1@rain.i-cable.com...[color=blue][color=green]
> >[color=darkred]
> >>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
> >>
> >>[/color]
> >
> >
> >[/color][/color]


  #6  
Old July 22nd, 2005, 11:24 AM
David Logan
Guest
 
Posts: n/a
Default Re: float and double question

Pat wrote:[color=blue]
> 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
>
>[/color]

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
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.