Please login or register.

Login with username, password and session length

News:

New to MS Chat? Why not introduce yourself!


Author Topic: Python: Fraction class (Converted from C++)  (Read 336 times)

Offline Ian

  • Administrator
  • Loyal Member
  • *****
  • Posts: 6,456
  • Browser: Firefox 3.6
  • Operating system: Windows 7 Home Premium
  • Processor: Intel Pentium 4 with HT
Python: Fraction class (Converted from C++)
« on: October 05, 2009, 09:39:25 PM »
So I learned Python today after I picked up a quick tutorial at school... So I thought I ought to test my l33t0rz 20 minute 'skills' by converting my C++ Fraction class, and I did! :D
 
Here is a code example:
Code: [Select]
Frac = Fraction(2, 4)
print Frac
Frac2 = Fraction(3, 4)
print Frac2
print Frac.add(Frac2)
print Frac.subtract(Frac2)
print Frac.multiply(Frac2)
print Frac.divide(Frac2)
print Frac.exp(3)

Would output:
Quote
1/2
3/4
5/4
1/-4
3/8
2/3
1/8

I think I might need to fix exp (Exponent) because, yeah, I don't think it is right, but correct me or prove me right that I am wrong (Weeee! Mind games :D )
 
Anyways, you can download the class below. Enjoy, I suppose.

Offline antimatter15

  • MS Chat Defender
  • Loyal Member
  • *****
  • Posts: 2,861
Re: Python: Fraction class (Converted from C++)
« Reply #1 on: October 06, 2009, 12:12:26 PM »
You'll learn eventually that Python does everything out of the box.

http://docs.python.org/library/fractions.html

Native Fraction Class
Ajax Animator, a web-based, collaborative animation authoring environment.

Offline Ian

  • Administrator
  • Loyal Member
  • *****
  • Posts: 6,456
  • Browser: Firefox 3.6
  • Operating system: Windows 7 Home Premium
  • Processor: Intel Pentium 4 with HT
Re: Python: Fraction class (Converted from C++)
« Reply #2 on: October 06, 2009, 01:48:18 PM »
Well, oh well :P Still needed practice.