Thread: fixed point arithmatic imp in c

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    99

    fixed point arithmatic imp in c

    I need to compare some monetary values. Fixed point is not supported in C. I know there is enough material out there to spend a few PhD's on (including a special GNU library), but it will bog me down too long. I am looking for a more pragmatic approach if possible.

    QUESTION: is there a simple pragmatic way in which seasoned programmers solve the lack of fixed point numbers in C? I have searched the forum here, but could not find too much about it. A link to a useful post will do fine.

    What do I want to do? Compare prices mainly and the occasional multiplication/addition/subtraction

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    int?

  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    99
    Hehe, thanks. I thought of it, instead of using floating point, I could simply use int + precision. Then you would somehow have to demand that all numbers are entered with the same precision and you could simply compary numbers. But this is exactly what I am looking for, some kind of bare bones approach that works. A few hints how this is is done will suffice, I don't want to reinvent the wheel.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You'll have to do a bit of parsing -- read in the number as a string; if they don't type a decimal point, then read it in and *100, otherwise read in the whole number part and the cents part and combine.

  5. #5
    Registered User
    Join Date
    Jul 2011
    Posts
    99
    Ok, so you simply have to write your own functions to compare, add, subtract and multiply strings made up of integers?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Why do you have strings made up of integers? There's no reason to have strings made up of integers. You have ints.

  7. #7
    Registered User
    Join Date
    Jul 2011
    Posts
    99
    I get it, I misred the strings part in your previous post. I know enough.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by django View Post
    Hehe, thanks. I thought of it, instead of using floating point, I could simply use int + precision. Then you would somehow have to demand that all numbers are entered with the same precision and you could simply compary numbers. But this is exactly what I am looking for, some kind of bare bones approach that works. A few hints how this is is done will suffice, I don't want to reinvent the wheel.

    Do everything in pennies... $1000.00 is simply 100,000 pennies.
    Code:
    // inputs
    int InputAsPennies( void )
     { float x;
        printf("Enter amount : $ ");
        scanf("%f",&x);
        return (int) x * 100); }
    
    
    // display dollars
    void DisplayAsDollars(int Amount)
      { printf("$ %d.%d", Amount / 100, Amount % 100); }
    Easy enough?
    Last edited by CommonTater; 09-05-2011 at 06:13 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fixed point opperations?
    By Devils Child in forum C++ Programming
    Replies: 5
    Last Post: 01-28-2008, 11:46 AM
  2. Fixed point MOD (for sin LUT)
    By Pea in forum C Programming
    Replies: 0
    Last Post: 04-10-2005, 06:03 PM
  3. Fixed Point Conversion
    By arkay in forum C Programming
    Replies: 1
    Last Post: 02-04-2005, 10:26 AM
  4. fixed point / floating point
    By confuted in forum Game Programming
    Replies: 4
    Last Post: 08-13-2002, 01:25 PM
  5. Floating point faster than fixed-point
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 11-08-2001, 11:34 PM