= #JxnPortable\docs\programmer_examples\commons_math_examples\BigFraction_UnitTest.jxn

#import org.apache.commons.math.fraction
#import org.apache.commons.math3.fraction
!
a = @BigFraction( 3, 4 )
b = @BigFraction( 2, 5 )
!
ut = @JxnUnitTest();
!
ut.assertEquals( 0.75, a.doubleValue() )
!
!
! fraction arithmetic:
!
c = a + b
ut.assertEquals( 23, c.getNumeratorAsLong() )
ut.assertEquals( 20, c.getDenominatorAsLong() )
!
c = a - b
ut.assertEquals(  7, c.getNumeratorAsLong() )
ut.assertEquals( 20, c.getDenominatorAsLong() )
!
c = a b
ut.assertEquals(  3, c.getNumeratorAsLong() )
ut.assertEquals( 10, c.getDenominatorAsLong() )
!
c = a / b
ut.assertEquals( 15, c.getNumeratorAsLong() )
ut.assertEquals(  8, c.getDenominatorAsLong() )
!
!
! mixed arithmetic with int or double:
!
c = a + 1
ut.assertEquals( 7, c.getNumeratorAsLong() )
ut.assertEquals( 4, c.getDenominatorAsLong() )
!
c = 1. - b
ut.assertEquals( 3, c.getNumeratorAsLong() )
ut.assertEquals( 5, c.getDenominatorAsLong() )
!
c = 2 a
ut.assertEquals( 3, c.getNumeratorAsLong() )
ut.assertEquals( 2, c.getDenominatorAsLong() )
!
c = a 2.
ut.assertEquals( 3, c.getNumeratorAsLong() )
ut.assertEquals( 2, c.getDenominatorAsLong() )
!
c = 2. / b
ut.assertEquals( 5, c.getNumeratorAsLong() )
ut.assertEquals( 1, c.getDenominatorAsLong() )
!
c = b / 2
ut.assertEquals( 1, c.getNumeratorAsLong() )
ut.assertEquals( 5, c.getDenominatorAsLong() )
!
!
! power of fraction:
!
ut.assertEquals( 1., ( a^0 ).doubleValue() )
!
ut.assertEquals( a, a^1 )
!
c = a^3
ut.assertEquals( 27, c.getNumeratorAsLong() )
ut.assertEquals( 64, c.getDenominatorAsLong() )
!
c = b^-2
ut.assertEquals( 25, c.getNumeratorAsLong() )
ut.assertEquals(  4, c.getDenominatorAsLong() )
!
ut.assertEquals( 0.5625, a^2. )
ut.assertEquals( 0.48713928962874675, a^2.5 )
!
c = a^20
ut.assertEquals( 3^20, c.getNumeratorAsLong() )
ut.assertEquals( 4^20, c.getDenominatorAsLong() )
!
!
! determine fraction for a double:
!
pi = @BigFraction( PI )
ut.assertEquals( PI, pi.doubleValue() )
!
!
! mixed array arithmectic:
!
ia = { 2, 3, 5 }
!
ca = ia - b
ut.assertEquals( JxnCast.toLong { 8, 13, 23 }, ca.getNumeratorAsLong() )
ut.assertEquals( 5,  @JxnUnroll ca.getDenominatorAsLong() )
!
ca = ia b
ut.assertEquals( JxnCast.toLong { 4, 6, 2 }, ca.getNumeratorAsLong() )
ut.assertEquals( JxnCast.toLong { 5, 5, 1 }, ca.getDenominatorAsLong() )
!
! BigFraction op ia not supported
expected = "term: org.apache.commons.math3.fraction.BigFraction * int[3] not supported";
ut.assertException( expected, $this.test "ca = b ia" )
expected = "term: org.apache.commons.math3.fraction.BigFraction / int[3] not supported";
ut.assertException( expected, $this.test "ca = a / ia" )
!
!
! check whether inputs a and b have been modified:
!
ut.assertEquals( 3, a.getNumeratorAsLong() )
ut.assertEquals( 4, a.getDenominatorAsLong() )
ut.assertEquals( 2, b.getNumeratorAsLong() )
ut.assertEquals( 5, b.getDenominatorAsLong() )
!
ut.summary()
JxnUnitTestFailureExpected.summaryAll( true )