• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

[JASS] Getting a fraction from a decimal

Status
Not open for further replies.
Ok, so i want a Fraction object, simply because I find it easier to work in fractions than decimals personally. I want a method to work out a fraction from a decimal. How would I do this? (I know it's possible because my calculator can do it)
JASS:
library Fraction
    struct Fraction
        integer num
        integer den
        real value
        
        static method create takes integer num, integer den returns Fraction
            local Fraction f = Fractionn.allocate()
            
            set f.num = num
            set f.den = den
            set f.value = I2R(num)/I2R(den)
            
            return f
        endmethod
        
        static method createFromReal takes real value returns Fraction
            local Fraction f = Fraction.allocate()
            
            set f.value = value
            //what here???
            
            return f
        endmethod
    endstruct
endlibrary
 
Level 7
Joined
Mar 8, 2009
Messages
360
What I should do is setting num = value*100 and den = 100.
And then trying to simplify(don't know good english word) the fraction. Maybe dividing num and den by numbers that give modulus 0 on both numbers. You could do that with a loop that checks all prime numbers from 2 till half of current den.
 
Status
Not open for further replies.
Top