Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do Clojure numbers end with "N" in the REPL?

Tags:

clojure

So, I grabbed the latest numeric tower for a couple quick calculations and noticed that the numbers returned have "N" at the end. Why? What does it mean?

clojure.math.numeric-tower=> (expt 64 20)
1329227995784915872903807060280344576N
clojure.math.numeric-tower=> (expt 36 20)
13367494538843734067838845976576N
like image 877
Brian Knoblauch Avatar asked Jun 05 '14 19:06

Brian Knoblauch


1 Answers

That is the literal form of BigInt:

user=> (type 1N)
clojure.lang.BigInt

versus, for example:

user=> (type 1)
java.lang.Long

or

user=> (type 1.0)
java.lang.Double

There's also the M suffix for BigDecimal.

user=> (type 1M)
java.math.BigDecimal

I'm not sure of all the rules for promotion to arbitrary precision (BigInt, BigDecimal). I think most of the "regular" math functions won't promote to arbitrary precision, but there are a few that do (e.g. +', -', *', inc', dec').

e.g. Regular + overflows:

user=> (+ Long/MAX_VALUE 1)
ArithmeticException integer overflow  clojure.lang.Numbers.throwIntOverflow (Numbers.java:1388)

but +' promotes:

user=> (+' Long/MAX_VALUE 1)
9223372036854775808N
like image 167
overthink Avatar answered Oct 17 '22 03:10

overthink