From: jzakiya@... Date: 2017-02-19T19:30:20+00:00 Subject: [ruby-core:79606] [Ruby trunk Feature#13219] bug in Math.sqrt(n).to_i, to compute integer squareroot, new word to accurately fix it Issue #13219 has been updated by Jabari Zakiya. After further testing I found the same errors when using **Math.sqrt(n).to_i** with large number when using ``(n**(1.0/2)).to_i``. This reinforces to me the need to provide **sqrt_i** (by whatever name) so users won't fall prey to this undocumented anomaly. ``` 2.4.0 :129 > n= 10**35; puts n, a = intsqrt4(n), a*a, b = Math.sqrt(n).to_i, b*b, c = (n**(1.0/2)).to_i, c*c 100000000000000000000000000000000000 316227766016837933 99999999999999999873578871987712489 316227766016837952 100000000000000011890233980627554304 316227766016837952 100000000000000011890233980627554304 => nil 2.4.0 :130 > ``` I also realized that the algorithm used to determine the integer squareroot can be easily generalized to correctly generate integer nth roots for arbitrary size integers. This capability will now allows Ruby to be used for many Number Theory and Cryptographic problems, such as generating correct integer values for eliptic curves, real integer roots of polynomials, public key, et al, encryption, for very large number sets. Because of this, I am going to add this capability to my **roots** rubygem. https://rubygems.org/gems/roots It has two (2) methods **root** and **roots** which generate all the n roots of an nth root for all **Numeric** types (integers, floats, complex, rational). In order to not clash with my proposed **class Integer** method name of **sqrt_i** as a Ruby core method, I will name two more **class Integer** methods **iroot2** and **irootn(n)** to be added to **roots**. Since finding squareroots is much more frequently done than other roots I give it its own method name. Below is the code for doing this (which I may refine). These methods will return the correct nth integer real (not complex) root for positive integers, and for odd roots of negative integers. ``` class Integer def irootn(n) return nil if self < 0 && n.even? raise "root n is < 2 or not an Integer" unless n.is_a?(Integer) && n > 1 sign = self < 0 ? -1 : 1 num = self.abs bits_shift = (num.bit_length)/n + 2 root, bitn_mask = 0, (1 << bits_shift) until (bitn_mask >>= 1) == 0 root |= bitn_mask root ^= bitn_mask if root**n > num end root*sign end def iroot2; irootn(2) end end ``` As with generating integer squareroots, doing ``(num**(1.0/n)).to_i`` produces incorrect answers for integers past some maximun value, as shown below. ``` 2.4.0 :129 > n = 10**37; puts n, a = n.irootn(3), a**3, b = (n**(1.0/3)).to_i, b**3 10000000000000000000000000000000000000 2154434690031 9999999999987694380850132072511299791 2154434690031 9999999999987694380850132072511299791 => nil 2.4.0 :130 > n = 10**38; puts n, a = n.irootn(3), a**3, b = (n**(1.0/3)).to_i, b**3 100000000000000000000000000000000000000 4641588833612 99999999999949657815157877549034676928 4641588833612 99999999999949657815157877549034676928 => nil 2.4.0 :131 > n = 10**39; puts n, a = n.irootn(3), a**3, b = (n**(1.0/3)).to_i, b**3 1000000000000000000000000000000000000000 10000000000000 1000000000000000000000000000000000000000 9999999999999 999999999999700000000000029999999999999 => nil 2.4.0 :134 > n = 10**112; puts n, a = n.irootn(4), a**4, b = (n**(1.0/4)).to_i, b**4 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 10000000000000000000000000000 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 9999999999999999583119736832 9999999999999998332478947328000104273492291412559539763672807300805169073432752214389506884609933472640769458176 => nil 2.4.0 :135 > ``` These methods are also preferable because they work correctly for negative integers, while the exponential root method throws errors. ``` 2.4.0 :150 > n = (10**112); puts n, a = n.irootn(3), a**3 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 21544346900318837217592935665193504952 9999999999999999999999999999999999999173635536969138804543654476122863556031297471619838179917400017798906449408 => nil 2.4.0 :151 > n = (10**112); puts n, b = (n**(1.0/3)).to_i, b**3 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 21544346900318734919579678222552399872 9999999999999857552405189045342271937486394088257690663380520853991701500619736949767614488813401623055562702848 => nil 2.4.0 :152 > n = -(10**112); puts n, a = n.irootn(3), a**3 -10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 -21544346900318837217592935665193504952 -9999999999999999999999999999999999999173635536969138804543654476122863556031297471619838179917400017798906449408 => nil 2.4.0 :153 > n = -(10**112); puts n, b = (n**(1.0/3)).to_i, b**3 RangeError: can't convert 1.077217345015937e+37+1.865795172362055e+37i into Integer from (irb):153:in `to_i' from (irb):153 from /home/jzakiya/.rvm/rubies/ruby-2.4.0/bin/irb:11:in `<main>' 2.4.0 :154 > ``` Again, creating C coded core versions of these methods not only will be more efficient but faster. ---------------------------------------- Feature #13219: bug in Math.sqrt(n).to_i, to compute integer squareroot, new word to accurately fix it https://bugs.ruby-lang.org/issues/13219#change-63033 * Author: Jabari Zakiya * Status: Open * Priority: Normal * Assignee: * Target version: ---------------------------------------- In doing a math application using **Math.sqrt(n).to_i** to compute integer squareroots of integers I started noticing errors for numbers > 10**28. I coded an algorithm that accurately computes the integer squareroot for arbirary sized numbers but its significantly slower than the math library written in C. Thus, I request a new method **Math.intsqrt(n)** be created, that is coded in C and part of the core library, that will compute the integer squareroots of integers accurately and fast. Here is working highlevel code to accurately compute the integer squareroot. ``` def intsqrt(n) bits_shift = (n.to_s(2).size)/2 + 1 bitn_mask = root = 1 << bits_shift while true root ^= bitn_mask if (root * root) > n bitn_mask >>= 1 return root if bitn_mask == 0 root |= bitn_mask end end def intsqrt1(n) return n if n | 1 == 1 # if n is 0 or 1 bits_shift = (Math.log2(n).ceil)/2 + 1 bitn_mask = root = 1 << bits_shift while true root ^= bitn_mask if (root * root) > n bitn_mask >>= 1 return root if bitn_mask == 0 root |= bitn_mask end end require "benchmark/ips" Benchmark.ips do |x| n = 10**40 puts "integer squareroot tests for n = #{n}" x.report("intsqrt(n)" ) { intsqrt(n) } x.report("intsqrt1(n)" ) { intsqrt1(n) } x.report("Math.sqrt(n).to_i") { Math.sqrt(n).to_i } x.compare! end ``` Here's why it needs to be done in C. ``` 2.4.0 :178 > load 'intsqrttest.rb' integer squareroot tests for n = 10000000000000000000000000000000000000000 Warming up -------------------------------------- intsqrt(n) 5.318k i/100ms intsqrt1(n) 5.445k i/100ms Math.sqrt(n).to_i 268.281k i/100ms Calculating ------------------------------------- intsqrt(n) 54.219k (�� 5.5%) i/s - 271.218k in 5.017552s intsqrt1(n) 55.872k (�� 5.4%) i/s - 283.140k in 5.082953s Math.sqrt(n).to_i 5.278M (�� 6.1%) i/s - 26.560M in 5.050707s Comparison: Math.sqrt(n).to_i: 5278477.8 i/s intsqrt1(n): 55871.7 i/s - 94.47x slower intsqrt(n): 54219.4 i/s - 97.35x slower => true 2.4.0 :179 > ``` Here are examples of math errors using **Math.sqrt(n).to_i** run on Ruby-2.4.0. ``` 2.4.0 :101 > n = 10**27; puts n, (a = intsqrt(n)), a*a, (b = intsqrt1(n)), b*b, (c = Math.sqrt(n).to_i), c*c 1000000000000000000000000000 31622776601683 999999999999949826038432489 31622776601683 999999999999949826038432489 31622776601683 999999999999949826038432489 => nil 2.4.0 :102 > n = 10**28; puts n, (a = intsqrt(n)), a*a, (b = intsqrt1(n)), b*b, (c = Math.sqrt(n).to_i), c*c 10000000000000000000000000000 100000000000000 10000000000000000000000000000 100000000000000 10000000000000000000000000000 100000000000000 10000000000000000000000000000 => nil 2.4.0 :103 > n = 10**29; puts n, (a = intsqrt(n)), a*a, (b = intsqrt1(n)), b*b, (c = Math.sqrt(n).to_i), c*c 100000000000000000000000000000 316227766016837 99999999999999409792567484569 316227766016837 99999999999999409792567484569 316227766016837 99999999999999409792567484569 => nil 2.4.0 :104 > n = 10**30; puts n, (a = intsqrt(n)), a*a, (b = intsqrt1(n)), b*b, (c = Math.sqrt(n).to_i), c*c 1000000000000000000000000000000 1000000000000000 1000000000000000000000000000000 1000000000000000 1000000000000000000000000000000 1000000000000000 1000000000000000000000000000000 => nil 2.4.0 :105 > n = 10**31; puts n, (a = intsqrt(n)), a*a, (b = intsqrt1(n)), b*b, (c = Math.sqrt(n).to_i), c*c 10000000000000000000000000000000 3162277660168379 9999999999999997900254631487641 3162277660168379 9999999999999997900254631487641 3162277660168379 9999999999999997900254631487641 => nil 2.4.0 :106 > n = 10**32; puts n, (a = intsqrt(n)), a*a, (b = intsqrt1(n)), b*b, (c = Math.sqrt(n).to_i), c*c 100000000000000000000000000000000 10000000000000000 100000000000000000000000000000000 10000000000000000 100000000000000000000000000000000 10000000000000000 100000000000000000000000000000000 => nil 2.4.0 :107 > n = 10**33; puts n, (a = intsqrt(n)), a*a, (b = intsqrt1(n)), b*b, (c = Math.sqrt(n).to_i), c*c 1000000000000000000000000000000000 31622776601683793 999999999999999979762122758866849 31622776601683793 999999999999999979762122758866849 31622776601683792 999999999999999916516569555499264 => nil 2.4.0 :108 > n = 10**34; puts n, (a = intsqrt(n)), a*a, (b = intsqrt1(n)), b*b, (c = Math.sqrt(n).to_i), c*c 10000000000000000000000000000000000 100000000000000000 10000000000000000000000000000000000 100000000000000000 10000000000000000000000000000000000 100000000000000000 10000000000000000000000000000000000 => nil 2.4.0 :109 > n = 10**35; puts n, (a = intsqrt(n)), a*a, (b = intsqrt1(n)), b*b, (c = Math.sqrt(n).to_i), c*c 100000000000000000000000000000000000 316227766016837933 99999999999999999873578871987712489 316227766016837933 99999999999999999873578871987712489 316227766016837952 100000000000000011890233980627554304 => nil 2.4.0 :110 > n = 10**36; puts n, (a = intsqrt(n)), a*a, (b = intsqrt1(n)), b*b, (c = Math.sqrt(n).to_i), c*c 1000000000000000000000000000000000000 1000000000000000000 1000000000000000000000000000000000000 1000000000000000000 1000000000000000000000000000000000000 1000000000000000000 1000000000000000000000000000000000000 => nil 2.4.0 :111 > n = 10**37; puts n, (a = intsqrt(n)), a*a, (b = intsqrt1(n)), b*b, (c = Math.sqrt(n).to_i), c*c 10000000000000000000000000000000000000 3162277660168379331 9999999999999999993682442519108007561 3162277660168379331 9999999999999999993682442519108007561 3162277660168379392 10000000000000000379480317059650289664 => nil 2.4.0 :112 > ``` -- https://bugs.ruby-lang.org/ Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe> <http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>