![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
Nick Guest
|
can I multiply a Hex value with a decimal? |
Posted: Sat Jan 22, 2005 6:14 pm |
|
|
It complies, but that doesnt mean it will work ;-)
is this a valid statment?
int x =3;
If ( x < (0x9E*(0.05)) )
{ } |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
Re: can I multiply a Hex value with a decimal? |
Posted: Sun Jan 23, 2005 4:00 am |
|
|
Nick wrote: | It complies, but that doesnt mean it will work ;-)
is this a valid statment?
int x =3;
If ( x < (0x9E*(0.05)) )
{ } |
It's sort of a valid statement, but may not do what you want. Assuming you want to compare 'x' with the integer value of the result, use a cast like:
Code: |
If ( x < (int8)(0x9E*0.05))
|
The compiler will in fact solve the foating point 'sum', since it only involves constants, at compile time, to generate the constant '7', and then carry out an integer comparison on this. If the comparison contains a variable, this will be solved at run time instead.
Without the cast, you are entering a 'black area', where the compiler can get confused about the types involved....
The result on the version without the cast, varies with compiler version. I tried two different versions, and one completely 'lost' the statement (look at the assembler). The cast is safer.
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|