# 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

## 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

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 21:04:55 +0200 )edit
{"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 1, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "url": "https://ask.sagemath.org/question/10414/mod-1-arithmetic/?sort=oldest", "fetch_time": 1723182463000000000, "content_mime_type": "text/html", "warc_filename": "crawl-data/CC-MAIN-2024-33/segments/1722640759711.49/warc/CC-MAIN-20240809044241-20240809074241-00685.warc.gz", "warc_record_offset": 85993306, "warc_record_length": 15238, "token_count": 1087, "char_count": 3273, "score": 3.5625, "int_score": 4, "crawl": "CC-MAIN-2024-33", "snapshot_type": "latest", "language": "en", "language_score": 0.6453971862792969, "file_path": "/sailhome/yjruan/project/latent-lm/data/processed/finemath-4plus/finemath-4plus/train-00055-of-00064.parquet"}