G25ET6HXYNPM2HJ2L76TGOLVV2LBDA3CIWCAN6SL2WKNTUG56RDQC
UJI7ISULWBM24NVDORMLOSCU5VQCCUUYDDKP7WYYMIPGJGOLRI3AC
2Q7SZHYMAFCYLG5MROYQ4BJ7HDIASPLIZJP7SM7BNX4GESUCZXJQC
VRPOSMITS7VRSIJU6YNEBELCUBTHPMPJD6F6F5PE35R2KECS42KAC
V7A5CRSQQY3WTZFNH73E6J4YOKE7YJMEW2XW7YJTM37UNTWRMZJQC
KNPO65LXKR2FL24QJZXFQNWH2O2ZF6QJGKTNVFVNBO4W27VT474AC
XBKUMBMDBZ37XISOWRJU3QRUUYP3TDT7MO6GYKTLCSTRPESASJTQC
PMF36FUOINXVVNP5XW6ZOFPPWFBRK62O4VCX45NE4Y4GDPL2Z3BQC
O6UAIBEXBNE2XMWQWU6YKGGRLDQFI364JMV6HCYCCI3LBQRD6RPQC
-- | The finite nimbers are a field of characteristic 2. There is no field homomorphism from the rationals to the nimbers, so @'fromRational'@ is always an error.
instance Fractional Nimber where
fromRational _ = error "Cannot map from field of characteristic 0 to characteristic 2"
recip 0 = error "Divide by zero"
recip 1 = 1
recip n =
let m = floorLog @Int $ floorLog n -- D = 2^2^m is the largest Fermat 2-power less than or equal to n
a = n `shiftR` bit m -- n = aD+b
aD = a `shiftL` bit m
b = n .^. aD
semiD = bit (bit m - 1) -- semimultiple of D
in (aD + a + b) / (semiD * a * a + b * (a + b))
-- | The finite nimbers are a field of characteristic 2. There is no field homomorphism from the rationals to the nimbers, so @'fromRational'@ is always an error.
instance Fractional Nimber where
fromRational _ = error "Cannot map from field of characteristic 0 to characteristic 2"
recip 0 = error "Divide by zero"
recip 1 = 1
recip n =
let m = floorLog @Int $ floorLog n -- D = 2^2^m is the largest Fermat 2-power less than or equal to n
a = n `shiftR` bit m -- n = aD+b
aD = a `shiftL` bit m
b = n .^. aD
semiD = bit (bit m - 1) -- semimultiple of D
in (aD + a + b) / (semiD * sqr a + b * (a + b))