Archive of RubyForge sup-talk mailing list
 help / color / mirror / Atom feed
* [sup-talk] Util - Fixnum num_digits is wrong
@ 2007-12-03  8:25 Ian Taylor
  2007-12-03 10:52 ` Shot (Piotr Szotkowski)
  0 siblings, 1 reply; 3+ messages in thread
From: Ian Taylor @ 2007-12-03  8:25 UTC (permalink / raw)


Probably wasn't causing any problems, but is wrong.

===================================================================
--- lib/sup/util.rb     (revision 737)
+++ lib/sup/util.rb     (working copy)
@@ -299,7 +299,7 @@
 class Fixnum
   def num_digits base=10
     return 1 if self == 0
-    1 + (Math.log(self) / Math.log(10)).floor
+    1 + (Math.log(self) / Math.log(base)).floor
   end
   
   def to_character

-- 
Ian Taylor


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [sup-talk] Util - Fixnum num_digits is wrong
  2007-12-03  8:25 [sup-talk] Util - Fixnum num_digits is wrong Ian Taylor
@ 2007-12-03 10:52 ` Shot (Piotr Szotkowski)
  2007-12-08 22:13   ` William Morgan
  0 siblings, 1 reply; 3+ messages in thread
From: Shot (Piotr Szotkowski) @ 2007-12-03 10:52 UTC (permalink / raw)


Ian Taylor:

>  class Fixnum
>    def num_digits base=10
>      return 1 if self == 0
> -    1 + (Math.log(self) / Math.log(10)).floor
> +    1 + (Math.log(self) / Math.log(base)).floor
>    end

Math.log and Float#/ returning Floats means the above
approach is prone to the IEEE 754 representation inaccuracy.

Wouldn?t the below make more sense?

class Fixnum
  def num_digits base = 10
    to_s(base).size
  end
end

-- Shot
-- 
> Who wants to recommend a Usenet reader for little ol' me?
I recommend Garrett Morris, particularly if you're hard-of-hearing.
      -- jennifer friedoff and Joseph Nebus, rec.arts.tv.mst3k.misc
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 191 bytes
Desc: not available
Url : http://rubyforge.org/pipermail/sup-talk/attachments/20071203/37b62dab/attachment.bin 


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [sup-talk] Util - Fixnum num_digits is wrong
  2007-12-03 10:52 ` Shot (Piotr Szotkowski)
@ 2007-12-08 22:13   ` William Morgan
  0 siblings, 0 replies; 3+ messages in thread
From: William Morgan @ 2007-12-08 22:13 UTC (permalink / raw)


Excerpts from Shot (Piotr Szotkowski)'s message of Mon Dec 03 02:52:10 -0800 2007:
> Math.log and Float#/ returning Floats means the above approach is
> prone to the IEEE 754 representation inaccuracy.
> 
> Wouldn?t the below make more sense?
> 
> class Fixnum
>   def num_digits base = 10
>     to_s(base).size
>   end
> end

Yes, but not as much as simply deleting the method. :)

-- 
William <wmorgan-sup at masanjin.net>


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2007-12-08 22:13 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-12-03  8:25 [sup-talk] Util - Fixnum num_digits is wrong Ian Taylor
2007-12-03 10:52 ` Shot (Piotr Szotkowski)
2007-12-08 22:13   ` William Morgan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox