anyone have a workaround for math.h inf limitation?

Discussion in 'Mac Apps and Mac App Store' started by GeeYouEye, Dec 3, 2003.

  1. GeeYouEye macrumors 68000

    GeeYouEye

    Joined:
    Dec 9, 2001
    Location:
    State of Denial
    #1
    I'm writing a C++ program to test a recursive function. The only problem is, I end up working with some pretty huge numbers, and quite quickly, I hit the infinity limit in math.h - 1e500. Does anyone know a way to work with arbitrarily large numbers, preferably in powers?
     
  2. whooleytoo macrumors 603

    whooleytoo

    Joined:
    Aug 2, 2002
    Location:
    Cork, Ireland.
    #2
    After 5 I start getting confused... :D

    But if you've no luck here, I'd recommend trying the Usenet groups (or Google Groups if you don't have a news server), especially comp.sys.mac.programmer.misc or comp.sys.mac.programmer.info. The guys there are very helpful.
     
  3. mainstreetmark macrumors 68020

    mainstreetmark

    Joined:
    May 7, 2003
    Location:
    Saint Augustine, FL
    #3
    What do you need to do with the number? Display them to the screen? Run them through another math function?

    Sometimes you can get away with shifting the decimal point to the left. It would decrease your accuracy (as digits fall off the right 'end') but the number could be several orders of magnitude away from +inf.

    You could also divide the textual display of the number in half or something, and manually connect them together. Huh? 1,234,567,890 could be represented as
    12,345 and 67,890, and if the 67,890 number ever goes beyond 99,999, you know to subtract 100,000 from the new number, and increment the 12,345 number by 1 (or whatever). You're expressing a 1e10 number as two 1e5 numbers, which is pretty convenient if your +inf was 1e10.
     
  4. Zeke macrumors 6502

    Joined:
    Oct 5, 2002
    Location:
    Greenville, SC
    #4
    Since you're doing C++ you could create a new class that you define to have a float and an integer. Then redefine mathematical operations to act directly on your class. So if you have 5.68245e600 (where 5.68245 is your float and 600 is your integer component) * 4.2852e300 then your multiplication operator is changed to work with your new class to multiply your floats 5.68245*4.2852 = 24.3504. Since it's greater than ten, divide by ten and add 1 to the 600+300. So now you've effectively increased infinity from the bounds of double float or whatever the highest precision is to 1e65000. That should be high enough. Not sure if this is where you want to go with it but I think it's pretty creative. This way you can deal with as large of numbers as you want and once you program everything once you can keep it if you ever need it.
     

Share This Page