1. No Profile Picture
Junior Member
Devshed Newbie (0 - 499 posts)

Join Date
Sep 2003
Posts
3
Rep Power
0

#### int aritmetic

hi,

I am stumped i am trying to do integer arithmetic but i need float precison. The problem is i am not allowed to cast or use float variable. I was wondering if anyone could give me a tip on how i could do this. Any hints will help

Thanks.
2. You could scale the numbers up. For example, if the calculation involves money, instead of treating the price of an item as \$1.99, you could treat it as 199 cents and work from there.

I have actually seen some code that did something like this for a handheld device (IIRC it was a Telxon PTC something or the other). Basically, the device didn't have an FPU unit in it and the previous programmer didn't want to use the floating point library, since it had limited memory. Instead he would scale up the numbers and when printing them out, he would scale back something like this:
Code:
```void printit(int amount)
{
int dollars, cents;
dollars = amount / 100;
cents = amount % 100;
printf("Amount is %d.%d", dollars, cents);
}```
Since I inherited this code, I pretty much did the same thing ;)
Last edited by Scorpions4ever; September 27th, 2003 at 10:41 AM.
3. You need to use fixed point arithmetic. Care must be taken with multiply and divide operations. Below is some code that demonstrates basic fixed-point operations, and conversion of fixed point values to a display string.

The number of decimal places can be changed by altering the macro FIX its value should be 10^n where n is the number of decimal places. In the fix_mul() and fix_div() functions I pass the fix value rather than depend on the FIX macro. This allows code to have mixed precision.

I have not considered the effect of overflow, or divide by zero. Beware that the range of fixed point values implemented like this is much more limited than for floating point. You could use long long to extend the range if your compiler supports it.

If you need to do a lot of fixed point arithmetic, then this is an ideal candidate for making into a class in C++. Operator overloading, and the overloading of the << and >> operators for iostream would make this code more readable, it could look just like any other built-in type.

PHP Code:
``` #include <stdio.h> #define FIX 1000   // three decimal places (10^3) // forward declarations int fix_mul( int val_a, int val_b, int fix ) ; int fix_div( int val_a, int val_b, int fix ) ; char* fixed_to_asc( char* buffer, int val, int fix ) ; int main(void) {     int val_a = 12345 ; // 12.345 * FIX     int val_b = 6791 ;  // 6.789 * FIX     int sum ;     int diff ;     int product ;     int quotient ;          char string_a[16] ;     char string_b[16] ;     char string_result[16] ;     sum = val_a + val_b ;     diff = val_a - val_b ;     product = fix_mul( val_a, val_b, FIX ) ;     quotient = fix_div( val_a, val_b, FIX ) ;     fixed_to_asc( string_a, val_a, FIX ) ;     fixed_to_asc( string_b, val_b, FIX ) ;          fixed_to_asc( string_result, sum, FIX ) ;     printf( "%s + %s = %s\n", string_a, string_b, string_result ) ;          fixed_to_asc( string_result, diff, FIX ) ;     printf( "%s - %s = %s\n", string_a, string_b, string_result ) ;     fixed_to_asc( string_result, product, FIX ) ;     printf( "%s * %s = %s\n", string_a, string_b, string_result ) ;     fixed_to_asc( string_result, quotient, FIX ) ;     printf( "%s / %s = %s\n", string_a, string_b, string_result ) ;     printf( "Press enter to finish" ) ;     getchar() ; } int fix_mul( int val_a, int val_b, int fix ) {     return( (val_a * val_b) / fix ) ; } int fix_div( int val_a, int val_b, int fix ) {     // add val_b/2 to scaled numerator for correct rounding     return( ((val_a * fix) + (val_b / 2)) / val_b ) ; } // fixed to ascii representation function char* fixed_to_asc( char* buffer, int val, int fix ) {     int intpart ;     int fractpart ;          // Calculate fractioal and integer parts     fractpart = val % fix ;     intpart = val / fix ;          // Create decimal string     sprintf( buffer, "%d.%d", intpart, fractpart ) ;          return( buffer ) ; }  ```