Connecting Tech Pros Worldwide Help | Site Map

Casting problem ?!

  #1  
Old December 2nd, 2005, 08:55 AM
Stranger
Guest
 
Posts: n/a
what is stored in "i" variable in this program ?

long i;
float y;
float number;
y = number;
i = * ( long * ) &y;

notice that "number" variable has been initialized ...

  #2  
Old December 2nd, 2005, 09:05 AM
Neelesh Bodas
Guest
 
Posts: n/a

re: Casting problem ?!


Stranger wrote:[color=blue]
> what is stored in "i" variable in this program ?
>
> long i;
> float y;
> float number;
> y = number;
> i = * ( long * ) &y;
>
> notice that "number" variable has been initialized ...[/color]

Where is the initialization of "number"? I nothing initialized, and y
assigned a junk value.
Also, please use C++-style casts - they are provided for a reason.
The result would be a junk value in "i".

  #3  
Old December 2nd, 2005, 09:05 AM
Mark
Guest
 
Posts: n/a

re: Casting problem ?!


i *think* he may have meant "note" that "number" has been initialized.
ie, elsewhere in his program that he has not shown.

however, i wouldnt know whats in i... why cant you just do
i = long(y)? or (long)y?

  #4  
Old December 2nd, 2005, 09:45 AM
Carlos Martinez Garcia
Guest
 
Posts: n/a

re: Casting problem ?!


The first N bytes of y.
N=sizeof(long)

The content of the first N bytes depends on how your computer an OS
represents floats in memory.

Stranger wrote:[color=blue]
> what is stored in "i" variable in this program ?
>
> long i;
> float y;
> float number;
> y = number;
> i = * ( long * ) &y;
>
> notice that "number" variable has been initialized ...
>[/color]
  #5  
Old December 2nd, 2005, 02:45 PM
mlimber
Guest
 
Posts: n/a

re: Casting problem ?!


Mark wrote:[color=blue]
> i *think* he may have meant "note" that "number" has been initialized.
> ie, elsewhere in his program that he has not shown.
>
> however, i wouldnt know whats in i... why cant you just do
> i = long(y)? or (long)y?[/color]

Perhaps because that was not the author's intent. If s/he wanted to
truncate y to the nearest whole number, s/he would have done just what
you said. If, however, s/he wanted an implementation-dependent,
bit-wise representation of y, then the nasty pointer casts may
accomplish that (depending on the sizes of the various data types).
Using reinterpret_cast would be preferred because it would signal
non-portable code.

Cheers! --M

  #6  
Old December 2nd, 2005, 03:05 PM
deane_gavin@hotmail.com
Guest
 
Posts: n/a

re: Casting problem ?!



mlimber wrote:[color=blue]
> Mark wrote:[color=green]
> > i *think* he may have meant "note" that "number" has been initialized.
> > ie, elsewhere in his program that he has not shown.
> >
> > however, i wouldnt know whats in i... why cant you just do
> > i = long(y)? or (long)y?[/color]
>
> Perhaps because that was not the author's intent. If s/he wanted to
> truncate y to the nearest whole number, s/he would have done just what
> you said. If, however, s/he wanted an implementation-dependent,
> bit-wise representation of y, then the nasty pointer casts may
> accomplish that (depending on the sizes of the various data types).
> Using reinterpret_cast would be preferred because it would signal
> non-portable code.[/color]

Isn't the OP's code

float y;
float number;
y = number;

undefined behaviour is number is not initialised before being assigned
to y?

Gavin Deane

  #7  
Old December 4th, 2005, 08:15 PM
Ron Natalie
Guest
 
Posts: n/a

re: Casting problem ?!


Carlos Martinez Garcia wrote:[color=blue]
> The first N bytes of y.
> N=sizeof(long)
>
> The content of the first N bytes depends on how your computer an OS
> represents floats in memory.
>[/color]

It might not be even that. There's no guarantee that float* can
be converted to long* with a cast.
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Newbie question: casting problem guncrew answers 1 March 12th, 2008 01:40 PM
Complex Casting problem in Generics Ajeet answers 7 January 19th, 2007 04:45 PM
casting problem in Datagrid - Option Strict in use fabrice answers 1 November 19th, 2005 06:35 PM
Casting Problem Carlo Marchesoni answers 6 November 18th, 2005 05:22 PM
casting problem (int * to int*) ghostdog answers 2 July 19th, 2005 06:33 PM