Re: Is there a bug in Matrix or Complex?

From: GOTO Kentaro <gotoken@...>
Date: 2002-08-05 23:14:23 UTC
List: ruby-core #299
At Tue, 6 Aug 2002 01:53:09 +0900,
Jim Freeze wrote:
> m
> Matrix[[1+2i, 3+4i], [4+2i, 3+4i]]
> m.to_f
> Matrix[[1.0+2.0i, 3.0+4.0i], [4.0+2.0i, 3.0+4.0i]]
> m.inv.inv
> Matrix[[1.0+2i, 3.0+4i], [4.0+2i, 3+4i]]
> m.inv.inv.to_f
> Matrix[[1.0+2i, 3.0+4i], [4.0+2i, 3+4i]]
> false
> 
> Why does taking the double inverse let the elements
> go back to integers? And, why does #to_f not convert
> each element to a float after taking the inverse?

Elements above are floating point numbers, not integers.  This odd
comes from implementation of Float#to_s which is called in `puts m'.

In ruby-1.6, x.to_s == sprintf("%.10g", x) for a Float x with 

  1.0e-3 <= |x| < 1.0e10.

"%.10g" doesn't format dot and following digits if number's flaction
part is not greater than 5e-11.  E.g.,

       #12345678901
  x = 2.00000000005
  x.to_s               #=> "2"
  sprintf("%.10g", x)  #=> "2"
  sprintf("%.11g", x)  #=> 2.0000000001

But in ruby-1.7,

        #12345678901
  puts 2.00000000005

generates 

  2.00000000005

-- Gotoken who advised to use "%.10g" :(

In This Thread

Prev Next