Behaviour of ruby with regards to numerical exactness (Scheme comparison)

In Ruby (1.9), are numbers always considered exact? I know Ruby does some fairly complex stuff with numbers under the hood, since you can do crazy things like ask for 1 trillion to the power of 1 trillion and you will get an answer (after waiting many moons for it to compute).

In Scheme, part of the specification dictates that implementations should indicate whether or not the implementation's internal representation of the number is "exact" or not. For example, 1/2 is always exact, 1/3 is exact, 0.3333 is exact etc etc. But the result of an imprecise mathematical operation on an exact number may produce a number that the Scheme implementation knows to be inexact (due to floating point precision).

(exact? (/ 0.33333 2))
=> #f

That's false, so it's not exact.

Is there a way to deduce the same information in Ruby? If I always use the Complex (or Rational) representation of a number during mathematical operations, will it always be exact in Ruby, or just extremely close to exact?

Complex("0.33333") / 2

Is that exact, or not?


Ruby's class Float is not exact. Class Integer on the other hand more or less "is" since it shifts seamlessly from Fixnum to Bignum

>> 1.4534346345235236346363574564356435
=> 1.45343463452352
>> 10**400
=> 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
>> 10.0**400
=> Infinity

It looks like others would love to see the test you are asking about, namely Numeric#exact? -- see this feature request.

Need Your Help

Get date by GPS using c-Sharp Xamarin

c# java android gps xamarin

I'm trying to get the current date through GPS.

How to query a field with ASCII 13 or “Carriage return”?

sql ms-access ascii

I Imported an XML file into a table.Then I tried to query the field that are not null. I have a field which seems to be blank or null but still returned in the query.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.