Thread: int aritmetic

    #1
  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. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,625
    Rep Power
    4247
    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.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,110
    Rep Power
    1803
    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_mulint val_aint val_bint fix ) ;
    int fix_divint val_aint val_bint fix ) ;
    charfixed_to_asccharbufferint valint 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_mulval_aval_bFIX ) ;
        
    quotient fix_divval_aval_bFIX ) ;

        
    fixed_to_ascstring_aval_aFIX ) ;
        
    fixed_to_ascstring_bval_bFIX ) ;
        

        
    fixed_to_ascstring_resultsumFIX ) ;
        
    printf"%s + %s = %s\n"string_astring_bstring_result ) ;
        
        
    fixed_to_ascstring_resultdiffFIX ) ;
        
    printf"%s - %s = %s\n"string_astring_bstring_result ) ;

        
    fixed_to_ascstring_resultproductFIX ) ;
        
    printf"%s * %s = %s\n"string_astring_bstring_result ) ;

        
    fixed_to_ascstring_resultquotientFIX ) ;
        
    printf"%s / %s = %s\n"string_astring_bstring_result ) ;


        
    printf"Press enter to finish" ) ;
        
    getchar() ;
    }

    int fix_mulint val_aint val_bint fix )
    {
        return( (
    val_a val_b) / fix ) ;
    }

    int fix_divint val_aint val_bint 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
    charfixed_to_asccharbufferint valint fix )
    {
        
    int intpart ;
        
    int fractpart ;
        
        
    // Calculate fractioal and integer parts
        
    fractpart val fix ;
        
    intpart val fix ;
        
        
    // Create decimal string
        
    sprintfbuffer"%d.%d"intpartfractpart ) ;
        
        return( 
    buffer ) ;


IMN logo majestic logo threadwatch logo seochat tools logo