View previous topic :: View next topic |
Author |
Message |
valemike Guest
|
cast "signed int" to "float"? Doesn't wo |
Posted: Wed Nov 10, 2004 8:07 am |
|
|
I tried the following:
Suppose i have the following float and signed int:
signed int x;
float y;
float z;
....
z = y + (float) x;
That's not going to give me desired values, right?
e.g.
y = 10.0
x = -4
z will not evaluate to something like 6.0 (or 5.99999), right? |
|
|
valemike Guest
|
|
Posted: Wed Nov 10, 2004 8:32 am |
|
|
I also found that:
unsigned int x;
float y;
float z;
If i then write the following:
z = y + (float) x;
It doesn't work right!
I have to break it up into two steps:
z = (float) x;
z = z + y;
Has anyone noticed that? |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Wed Nov 10, 2004 10:45 am |
|
|
What answer does it give you?
Also what complier version are you using?
I am using 3.104 with 16C771 chips. I do lots of casting and it all works fine. But I don't use floats. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
Ttelmah Guest
|
Re: cast "signed int" to "float"? Doesn' |
Posted: Wed Nov 10, 2004 10:55 am |
|
|
valemike wrote: | I tried the following:
Suppose i have the following float and signed int:
signed int x;
float y;
float z;
....
z = y + (float) x;
That's not going to give me desired values, right?
e.g.
y = 10.0
x = -4
z will not evaluate to something like 6.0 (or 5.99999), right? |
Wat you post should work, and has worked OK for me in the past. However I'd probably bracket the cast to seperate it from the arithmetic.
So:
z=y+((float)x);
I have seen problems in the past, with the arithmetic, seeming to get confused by the cast, unless the extra brackets are added.
What are you seeing?.
Best Wishes |
|
|
valemike Guest
|
|
Posted: Wed Nov 10, 2004 11:15 am |
|
|
I wish I could pull this whole topic thread
I realized that i forgot to read the result from eeprom after a hard reset. False alarm! Sorry.
I couldn't verify the result myself because the unit i'm working uses rb6 and rb7, nor does it have a MAX232 with which to connect to Hyperterminal. |
|
|
Ttelmah Guest
|
|
Posted: Wed Nov 10, 2004 11:23 am |
|
|
valemike wrote: | I wish I could pull this whole topic thread
I realized that i forgot to read the result from eeprom after a hard reset. False alarm! Sorry.
I couldn't verify the result myself because the unit i'm working uses rb6 and rb7, nor does it have a MAX232 with which to connect to Hyperterminal. |
It happens.
Why though did you think that seperating the lines was making it work, if you couldn't really test it?.
You can allways output a 'diagnostic' byte at a low baud rate, on any pinm and latch this on a digital scope, or simply flash an LED really slowly, and count the pulses, if you have even one pin available.
Best Wishes |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Wed Nov 10, 2004 11:39 am |
|
|
Well if you are using MPLAB, there is a simulator in there that works just great for these tests. That is how I would have tested it. |
|
|
|