# mod 1 arithmetic

Hi all,

is it possible, to do simple mod 1 evaluations? For example, Mathematica outputs

N[1/2*(1 + Sqrt[5])^100 mod 1] ~ 0.9999999999999999999987374...


but

n(Mod(1/2*(1 + sqrt(5))^100,1))


(or similar attempts) in Sage result in

TypeError: unable to convert x (=1/2*(sqrt(5) + 1)^100) to an integer


Cheers, Markus

edit retag close merge delete

Sort by » oldest newest most voted

The function Mod is intended to be used with integers. In order to use mod 1 operations you should use % (which stands for remainder of the euclidean division) as in

sage: sage: 5.3 % 1.0
0.300000000000000


The operation Mod is to be used when you want to work with integers mod N (ie the ring $\mathbb{Z} / N \mathbb{Z}$).

Note that the following works

sage: K.<sqrt5> = NumberField(x^2-5,'sqrt5',embedding=1)
sage: sqrt5 % 1.0
0.236067977499790


But not this one

sage: sqrt(5) % 1.0
Traceback (most recent call last):
...
TypeError: unsupported operand type(s) for %: 'sage.symbolic.expression.Expression' and 'sage.symbolic.expression.Expression'

more

Note that % 1.0 gives results in [-0.5,0.5] rather than [0,1]. If you prefer values in [0,1], add 1 when you get a negative result.

sage: a = sqrt(3.); a
1.73205080756888
sage: b = a % 1.0; b
-0.267949192431123
sage: b + 1
0.732050807568877


Also, the precision of the real numbers that Sage will use to compute modulo 1 can be augmented by using more trailing zeros. Example:

sage: x = polygen(ZZ)
sage: K.<r5> = NumberField(x^2 - 5, embedding=2)
sage: c = (1 + r5)^100 / 2
sage: c % 1.0
0.000000000000000
sage: c % 1.000000000000000000000000000000000000000000000000000000000000
-0.396388003719039261341094970703125000000000000000000000000000

( 2013-08-05 14:04:55 -0600 )edit

## In short

If you are interested in elements of a number field, like in your example with sqrt(5), you could do this:

sage: K.<a> = NumberField(x^2-5,'a',embedding=2.236)
sage: b = (1 + a)^100 / 2
sage: RR(b)
5.02034537778533e50


You see that b is roughly 5 * 10^50 so you want to compute with more than 50 decimal digits precision. In particular, 50 hexadecimal digits, ie 200 binary digits, would work.

sage: R = RealField(prec=200)
sage: R(b)
5.0203453777853342471068924069687121743561926939248060361200e50
sage: R(b).frac()
0.60361199639737606048583984375000000000000000000000000000000


This gives you an idea of the value modulo one. Exercise: how many digits are correct?

## More details

We create the embedded number field K for sqrt(5).

sage: K.<a> = NumberField(x^2-5,'a',embedding=2.236)


Check that the generator squares to 5.

sage: a^2
5


Check that the embedding is the one with the positive square root of 5.

sage: a.is_real_positive()
True


Or:

sage: RR(a)
2.23606797749979


Compute b in K.

sage: b = (1+a)^100/2
sage: b
112258335352548699824296575003536617685648198860800*a +
251017268889266712355344620348435608717810034802688


Check how big it is.

sage: RR(b)
5.02034537778533e50


Work with the appropriate precision.

sage: R = RealField(prec=200)
sage: R(b)
5.0203453777853342471068924069687121743561926939248060361200e50
sage: R(b).frac()
0.60361199639737606048583984375000000000000000000000000000000


Hint for the exercise:

sage: RR(2^200/10^50)
1.60693804425899e10

more