Math with Huge (infinite) numbers

require ‘bigdecimal’
x=(Math.sqrt(9999999999999999*9999999999999999))
=> 100000000000000.0. #wrong! Should be 9999999999999999

Puts x*x
=> 1.0e+32

Puts “%d” % (x*x)
=> 1000000000000009999999988654363748

I have been performing math with large numbers… This seems to be
limited by the hardware and operating system for what size a number can
be accurately processed… the example above shows at what point my
android device fails to provide accurate results…

are there any libs that can accurately process insanely large numbers?

Maybe Bigdecimal can but I’m using it wrong?

Puts Math.sqrt(999999)=> 999
Puts BigDecimal.new(999
999).sqrt(16)=>0.999E3

So. I’m posting this example, I don’t understand the (16) part

I don’t see the benefit of using Bigdecimal… the output is not useful
for this… or I’m not doing something right…

On my labtop and on my android phone. I can perform sqrt at the most of
15 in length

X=999999999999999
Y=9999999999999999
Math.sqrt(xx)=>999999999999999
Math.sqrt(y
y)=>1000000000000000

Y is 16 bytes long and fails to show proper results.
I would like to perform math with even bigger numbers.

BigDecimal has its own #sqrt method.

When you define a bigdecimal number I highly suggest you use quotation
marks.

[32] pry(main)> x = BigDecimal.new(“999999999999999999”)
=> #<BigDecimal:25e8df8,‘0.9999999999 99999999E18’,18(27)>
[33] pry(main)> (x*x).sqrt(20).to_i
=> 999999999999999999

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs