clojure 2026-01-13

Should multiplying 2 nonzero numbers ever return 0?

(* 3.4857115413072264E-278 2.5286959697152756E-199)
; => 0.0

(list (int 3.4857115413072264E-278) (int 2.5286959697152756E-199))
huh?

int casts the value to a int

user=> (.toPlainString (bigdec 3.4857115413072264E-278))
"0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000034857115413072264"
so you are left with 0 which makes sense?

👍 2

In the first case, you lose the precision that can fit in a double so you're left with a 0.0.

user=> (* (bigdec 3.4857115413072264E-278) (bigdec 2.5286959697152756E-199))
8.81430472609360480238583274759584E-477M
user=> (.toPlainString (* (bigdec 3.4857115413072264E-278) (bigdec 2.5286959697152756E-199)))
"0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000881430472609360480238583274759584"

Yes, it's floating point arithmetic 101.

Also, (a + b) + c does not always equal a + (b + c) (again, you can only cram a limited precision into X bytes)

Thanks! E notation confused me

In general, if you need to work with numbers of hugely different scales, you need to use logarithms.

Also, lots of accumulation can bite you...

I was just running generative tests on a function that does IOU for a model's detections

So in that case there won't ever be such small values. But I added a check in the function just in case anyway

E-477 is zero for all practical applications. 😎😜

➕ 3
😂 4

Even E-16

➕ 1