logo
down
shadow

Clojure Integer Overflow Using reduce function


Clojure Integer Overflow Using reduce function

By : user2955430
Date : November 22 2020, 10:40 AM
like below fixes the issue I am currently going through the project euler problems using Clojure. The second question requires you to sum the first 4000000 fibonaci numbers. Unfortunately my code is giving me an Integer Overflow exception. , use +' instead of + to get auto promoting addition to bigintegers
code :
(reduce +' 
   (filter even? 
     (take n (map first (iterate (fn [[x y]] [y (+' x y)]) [0 1])))))


Share : facebook icon twitter icon
Not getting integer overflow in Clojure?

Not getting integer overflow in Clojure?


By : Muyembe
Date : March 29 2020, 07:55 AM
This might help you (edited) Clojure (at least 1.3.0) automatically converts the integer to a long if necessary. For more details about automatic boxing, promotion and primitive numeric type support in Clojure 1.3.0 check the Documentation for Clojure 1.3.0 Numerics.
The reason you don't get an overflow is because Clojure automatically converts the integer to a long, so (+ Integer/MAX_VALUE Integer/MAX_VALUE) is adding two longs:
code :
user> (type Integer/MAX_VALUE)
java.lang.Long
Integer overflow using lazy sequences in Clojure

Integer overflow using lazy sequences in Clojure


By : Sultan Hasanudin
Date : March 29 2020, 07:55 AM
Hope this helps Filtering an infinite seq produces an infinite seq and reducing over this causes filter to keep looking for another matching item even after the predicate stops returning true.
Replace filter with take-while. The infinite sequence generated by (fib) will cause filter to run forever, but before that it will break due to the ArithmeticException you're experiencing. take-while will stop further evaluation of the list after the (fn [x] (< x n)) predicate evaluates to false.
code :
(defn up-to [n seqn]
  (take-while (fn [x] (< x n)) seqn))

(sum (up-to 100 (fib))) ;; => 232
Clojure integer overflow

Clojure integer overflow


By : EvaN
Date : March 29 2020, 07:55 AM
Hope that helps That's strange, I see different results with Clojure 1.4.0and Java(TM) SE Runtime Environment (build 1.7.0_06-b24), on Ubuntu 12.04 64bit:
code :
user=> *clojure-version*
{:major 1, :minor 4, :incremental 0, :qualifier nil}
user=> (+ Integer/MAX_VALUE Integer/MAX_VALUE)
4294967294
user=> (type 1)
java.lang.Long
user=> (def max-plus-one (+ Integer/MAX_VALUE one))
#'user/max-plus-one
user=> max-plus-one
2147483648
user=> (type max-plus-one)
java.lang.Long
user=> (+ Integer/MAX_VALUE Integer/MAX_VALUE)
4294967294
(defn +
  "Returns the sum of nums. (+) returns 0. Does not auto-promote
  longs, will throw on overflow. See also: +'"
  {:inline (nary-inline 'add 'unchecked_add)
   :inline-arities >1?
   :added "1.2"}
  ([] 0)
  ([x] (cast Number x))
  ([x y] (. clojure.lang.Numbers (add x y)))
  ([x y & more]
     (reduce1 + (+ x y) more)))
final public Number add(Number x, Number y){
    return num(Numbers.add(x.longValue(),y.longValue()));
}
user=> *clojure-version*
{:major 1, :minor 2, :incremental 1, :qualifier ""}
user=> (def one 1)
#'user/one
user=> (class 1)
java.lang.Integer
user=> (def max-plus-one (+ Integer/MAX_VALUE one))
#'user/max-plus-one
user=> max-plus-one
2147483648
user=> (class max-plus-one)
java.lang.Long
user=> (+ Integer/MAX_VALUE Integer/MAX_VALUE)
java.lang.ArithmeticException: integer overflow (NO_SOURCE_FILE:0)
How can i solve integer overflow in Clojure?

How can i solve integer overflow in Clojure?


By : mr.cool
Date : March 29 2020, 07:55 AM
I wish this help you Change + to +'. That will automagically get you a clojure.lang.BigInt if the result doesn't fit into a long. You can also use the N suffix on literals to get a BigInt.
code :
(class (+' 3 2)) ;=> java.lang.Long
(class (+' 300000000000000000000000000000 2)) ;=> clojure.lang.BigInt
(class 3N) ;=> clojure.lang.BigInt
Clojure reduce function

Clojure reduce function


By : Sean
Date : March 29 2020, 07:55 AM
I wish did fix the issue. The first argument (%1 in the anonymous function) to the function passed to reduce is the accumulator, which is initally the empty map {} passed as the second argument to reduce. Maps are functions which lookup the value for the given key, returning the optional default if the key is not found e.g.
code :
({"word" 1} "word") = 1
({"word" 1} "other" 0) = 0
(%1 %2 0)
#(assoc %1 %2 (inc (%1 %2 0))
shadow
Privacy Policy - Terms - Contact Us © ourworld-yourmove.org