View Single Post
  #3   Spotlight this post!  
Unread 15-01-2007, 21:25
Dave Flowerday Dave Flowerday is offline
Software Engineer
VRC #0111 (Wildstang)
Team Role: Engineer
 
Join Date: Feb 2002
Rookie Year: 1995
Location: North Barrington, IL
Posts: 1,366
Dave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond reputeDave Flowerday has a reputation beyond repute
Re: Floating Points and rounding

Quote:
Originally Posted by JBotAlan View Post
You aren't *supposed* to use floats, because the processor doesn't natively do floating point calculations, but the compiler spits out code that works.
The problem here is that on a processor that supports floating-point math, doing any floating-point arithmetic takes a single assembly operation. On processors that don't support it (like the RC processor), it has to be done in software. So, each time you do anything with a floating point number, the compiler will end up generating a LOT of code to do it (something like 100 lines of assembly instead of just one). This uses up a lot of extra code space and is obviously a lot slower.

The answer has already been provided here. If you need 1/10 accuracy, just multiply all of your numbers by 10. Also, if you're trying to multiply something by a fraction, but the result only needs to be an integer, keep this trick in mind. If you're multiplying by a fraction, you can multiply by the numerator first and then divide by the denominator afterwards to avoid floating point (just be careful of overflows). So when someone here was talking about multiplying by 180/127, do this instead:

(x * 180) / 127