Sign in

username:

password:



Not a member?

Search rabbit-semi



Search tips

Subscribe to rabbit-semi



Ads

Discussion Groups

Discussion Groups | Rabbit-Semi | A question about float to int casting.

This is a group for folks designing and programming embedded systems using the Rabbit Semiconductor C-programmable microcontroller. Rabbit Semi is a spin-off from Z-World who makes a variety of embedded modules and tools. This group is not affiliated with either Rabbit or Z-World, but is a user forum for sharing ideas, asking questions, flaunting knowledge, and other typical user group stuff. The Rabbit is a powerful uC, supported by a full-featured C-compiler.

A question about float to int casting. - Julian Higgison - May 26 23:22:41 2008

Hi all!

I'm kinda new here, but have been lurking for a while and mucking about on
and off with other people's rabbit code for a bit... adding extra
features/options here and there, etc. pretty simple stuff, really.

Anyway - I'm having some confusion trying to work something out today.

I try to avoid floats wherever I can in any 8 bit code I write, in favour o=
f
scaled values in ints - so I don't have much experience working with floats
- but I understand from searching here that, at least in DC, to cast from a
float to an int it's necessary to add half an LSB of the int (ie, 0.5) to
your float, because otherwise the casting will just be a truncation of the
value of the float, throwing out any decimal portion of the number.

Anyway - I have some code here that does this:
logData.depthA[ESACount] =3D (int)((DepthA * 100) + 0.01); //28/07/04 Fixed
float casting rounding issue

What I'm trying to work out, is why would we want to add 0.01 here? Would
this casting be more correct (as in the minimum value of max error due to
casting over a series of changing data) if we added 0.5 instead of 0.01?

The code is in DC8.30 if that makes any difference.
Thanks,

Julian Higginson
Bruttour International P/L
Tel :=A0 +61 2 9987 1581
www.bruttour.com.au

------------------------------------



(You need to be a member of rabbit-semi -- send a blank email to rabbit-semi-subscribe@yahoogroups.com )