= #JxnPortable/docs/programmer_examples/demos/PluggableDemo.jxn


#function $test( plug )
!
JxnPrimitiveWrapper.setPluggable plug
!
#if not plug.equals "Java"
! JXN changes type to avoid overflow:
#else
! Java: result of Math.pow(...) is always double:
#endif
2^30
2^31
2^62
2^63
!
#if plug.equals "Java"
! possible overflow:
#endif
Integer.MIN_VALUE - 1
Integer.MIN_VALUE + 1
Integer.MAX_VALUE - 1
Integer.MAX_VALUE + 1
Byte.MIN_VALUE - (1).byteValue()
Byte.MIN_VALUE + (1).byteValue()
Byte.MAX_VALUE - (1).byteValue()
Byte.MAX_VALUE + (1).byteValue()
Long.MIN_VALUE - 1
Long.MIN_VALUE + 1
Long.MAX_VALUE - 1
Long.MAX_VALUE + 1
!
#if not plug.equals "Java"
! JXN switches to double to preserve accuracy:
#else
! Java: fractional part of the result is truncated:
#endif
11/4  
12/4
!
!
! byte arithmetic:
#if not plug.equals "Java"
! - Jxn: byte op byte -> byte, x^y -> int -> long -> double
#else
! - Java: byte op byte -> int,  x^y -> Math.pow(double,double) -> double
#endif
bx = (7).byteValue()
by = (5).byteValue()
bx + by
bx - by
bx * by
(15).byteValue() / by
bx / by
bx % by
bx ^ by
!
!
#if not plug.equals "Java"
! JXN supports boolean operations:
#else
! Java: boolean operations not supported:
#endif
b0 = false
b1 = true
b4x = { false, false, true, true }
b4y = { false, true, false, true }
!
-b4y  ! not
b4x + b4y  ! or
b4x b4y  ! and
b4x^b4y  ! exor
not( b4y )
or( b4x, b4y )
and( b4x, b4y )
exor( b4x, b4y )  ! exor == ne
ne( b4x, b4y )
eq( b4x, b4y )
!
#if not plug.equals "Java"
! - boolean operations not supported:
#endif
b0 - b1
b0 / b1
b0 % b1
!
! - mixed operations not supported:
b1 + 1
1 + b1
b1 2
2 b1
b1^2
2^b1
!
#if not plug.equals "Java"
! - JXN: implicit cast of boolean argument:
#else
! - Java: cast of boolean argument not supported:
#endif
pow( b1, 2 )
pow( 2, b1 )
!
! - explicit cast always supported for JxnPrimitiveWrapper:
b0.doubleValue()
b1.doubleValue()
(5).booleanValue()
(0.).booleanValue()
!
#return
#endfunction
$test.setDebug(1)


oldDebug = $this.setDebug false


$test( "Safe" )  ! --- running $test for jxn safe arithmetic ---


$test( "Java" )  ! --- running $test for Java arithmetic ---


JxnPrimitiveWrapper.setPluggable ""  ! reset to default

$this.setDebug oldDebug