Numbers Fixnum + Bignum = just Integer?

L

Leszek Dubiel

I am looking for scripting language to replace Perl, which I am using
now. I don't like Perl, because its awful syntax. I thought about
Python when I've heard of Ruby. I study Ruby now, it seems to be
excellend (everything is object) and I have one question.

Why do you have two classes of integers? I think there are only two
solutions for numbers in a good language:

1. Numbers can be as big as machine word. To use bigger ones module
should be included.

2. Nubers can be as big as programmer wants, but DON'T suply two
classes for the same THING. There should be one class to rule them all
-- when number is small it is just one word, when it gets bigger it is
a list. Programmer wants to use numbers and don't want to care about
implementation. If this is so in Ruby, then using two classes is not
good and that classes should be hidden to user (programmer). If Ruby
will have implemented BigNum, Fixnum, then to be complete it sould
implement BigFload (huge precission), Complex, Naturals....

I would like to be convinced that Ruby solution is good -- now I don't
see any reason for having two classes and I feel that some time in the
future this would be a problem. Good languages are good at the
beginning, but after few years they are patched, and patched -- they
are getting worse and worse. (For example "use strict" in perl -- it
seems to me as a patch that prevents programmer from doing something
wrong...)
 
K

Kent Dahl

Leszek said:
I am looking for scripting language to replace Perl, which I am using
now. I don't like Perl, because its awful syntax. I thought about
Python when I've heard of Ruby. I study Ruby now, it seems to be
excellend (everything is object) and I have one question.

Why do you have two classes of integers? I think there are only two
solutions for numbers in a good language:

1. Numbers can be as big as machine word. To use bigger ones module
should be included.

2. Nubers can be as big as programmer wants, but DON'T suply two
classes for the same THING. There should be one class to rule them all
-- when number is small it is just one word, when it gets bigger it is
a list.

.... which is kind of what FixNum and BigNum do under the hood. When the
number is small (31 bits), it stays FixNum. When a resulting computation
with a FixNum is too big, a BigNum is returned instead. You have no need
to instantiate FixNums and BigNums explicitly.

I think the more correct thing to say for Ruby is that it supplies one
TYPE (integers) that is implemented using two CLASSES (FixNum and
BigNum) for efficiency.
Programmer wants to use numbers and don't want to care about
implementation. If this is so in Ruby, then using two classes is not
good and that classes should be hidden to user (programmer).

Too much hiding is like training wheels and would turn the experts off.
It is already pretty hidden in actual usage.
If Ruby
will have implemented BigNum, Fixnum, then to be complete it sould
implement BigFload (huge precission),

require 'bigdecimal' # Check.

require 'complex' # Check.
Naturals....

Natural numbers are non-negative integers, so FixNum and BigNum cover
them fairly well. Were you thinking of rational numbers perhaps or am I
missing something obvious?

I would like to be convinced that Ruby solution is good -- now I don't
see any reason for having two classes and I feel that some time in the
future this would be a problem. Good languages are good at the
beginning, but after few years they are patched, and patched -- they
are getting worse and worse. (For example "use strict" in perl -- it
seems to me as a patch that prevents programmer from doing something
wrong...)

The two classes are a good solution as far as it is transparent. You can
add and multiply numbers, not having to worry about the boundary of a
FixNum: If you break the boundary, results appear as BigNums, which
although slower and more memory hungry, give the right answer.
 
K

Kent S.

I am looking for scripting language to replace Perl, which I am using
now. I don't like Perl, because its awful syntax. I thought about
Python when I've heard of Ruby. I study Ruby now, it seems to be
excellend (everything is object) and I have one question.

Why do you have two classes of integers? I think there are only two
solutions for numbers in a good language:

1. Numbers can be as big as machine word. To use bigger ones module
should be included.

2. Nubers can be as big as programmer wants, but DON'T suply two
classes for the same THING. There should be one class to rule them all
-- when number is small it is just one word, when it gets bigger it is
a list. Programmer wants to use numbers and don't want to care about
implementation. If this is so in Ruby, then using two classes is not
good and that classes should be hidden to user (programmer). If Ruby
will have implemented BigNum, Fixnum, then to be complete it sould
implement BigFload (huge precission), Complex, Naturals....

Don't you think that you contradict yourself? You said programmer
shouldn't care about the implementation. As long as Fixnum and Bignum
both have the same interface (that is the same set of methods with
identical signature) you shouldn't care about which class a particular
instance has. In Ruby a class of an instance does not define this
instance's type. Only methods a particular instance responds to define
its type. It's called 'duck typing'.
I would like to be convinced that Ruby solution is good -- now I don't
see any reason for having two classes and I feel that some time in the
future this would be a problem. Good languages are good at the
beginning, but after few years they are patched, and patched -- they
are getting worse and worse. (For example "use strict" in perl -- it
seems to me as a patch that prevents programmer from doing something
wrong...)

I don't think this is happening with Ruby though.

Cheers,
Kent.
 
M

Mark Hubbart

I am looking for scripting language to replace Perl, which I am using
now. I don't like Perl, because its awful syntax. I thought about
Python when I've heard of Ruby. I study Ruby now, it seems to be
excellend (everything is object) and I have one question.

Why do you have two classes of integers? I think there are only two
solutions for numbers in a good language:

1. Numbers can be as big as machine word. To use bigger ones module
should be included.

2. Nubers can be as big as programmer wants, but DON'T suply two
classes for the same THING. There should be one class to rule them all
-- when number is small it is just one word, when it gets bigger it is
a list. Programmer wants to use numbers and don't want to care about
implementation.

Ruby uses Duck typing, so in nearly all cases, Bignums are
indistinguishable from Fixnums; you wouldn't know you had one until you
checked it's class. The conversion is essentially transparent, and the
interface is the same. This basically makes it so that the programmer
doesn't even need to think about the numbers, and how big they might be
getting; the interpreter takes care of all that for them.
If this is so in Ruby, then using two classes is not
good and that classes should be hidden to user (programmer). If Ruby
will have implemented BigNum, Fixnum, then to be complete it sould
implement BigFload (huge precission), Complex, Naturals....

These are actually numeric types that are defined in the standard
library. if your require 'bigdecimal' and 'mathn', you should get all
the numeric types available. Again, they are completely transparent:

# rational numbers
1/4 * 2/3
#=>1/6
# complex numbers
Math.sqrt(-1) + 1
#=>Complex(1, 1)

When you have included the module, rational numbers are created
whenever you divide an integer by an integer where it doesn't come out
even. Taking the square root of any negative number will automatically
create a complex number for you.

I would like to be convinced that Ruby solution is good -- now I don't
see any reason for having two classes and I feel that some time in the
future this would be a problem. Good languages are good at the
beginning, but after few years they are patched, and patched -- they
are getting worse and worse. (For example "use strict" in perl -- it
seems to me as a patch that prevents programmer from doing something
wrong...)

The handling of Numeric types in Ruby seems to have been designed for
being resistant to patching problems. In fact, if you have a special
numeric type that you need to use, you can write your own! Inherit from
Numeric, set up how the math should work; wrap any appropriate methods
from Integer and Float so they generate your numeric type when specific
types of math are done, and you have it whipped. Once you load your
library, you can begin using the built-in numeric literals to do
whatever specific math you want, all transparently.

cheers,
--Mark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top