log10_digit_by_digit.sf 376 B

123456789101112131415161718192021
  1. #!/usr/bin/ruby
  2. # Based on code by @catb0t
  3. # https://github.com/trizen/sidef/issues/37
  4. # Computing Logarithms Digit-by-Digit:
  5. # https://brics.dk/RS/04/17/BRICS-RS-04-17.pdf
  6. func Log10 (x, prec=48) {
  7. var k = x.float
  8. gather {
  9. prec.times {
  10. take(var d = k.ilog10)
  11. k = (k / 10**d)**10
  12. }
  13. }
  14. }
  15. say log10(1234.56)
  16. say Log10(1234.56).insert(1, '.').join