Why is there a seperate Math class?

Why does everything have to be Math.<func>(num)? Isn't num.func more object oriented-ish?

For example:
>> (Math.methods - Object.methods).select {|x| Math.method(x).arity == 1}.each do |x|
?> Numeric.class_eval { ?> define_method(x.to_sym) { Math.send(x.to_sym, self) } >> }
>> end
=> ["tan", "frexp", "sinh", "exp", "acos", "tanh", "log", "asin", "acosh", "cos", "log10", "atan", "erf", "asinh", "sin", "sqrt", "cosh", "erfc", "atanh"]
>> *5.sqrt*
=> 2.23606797749979
>> *Math.sqrt(5)*
=> 2.23606797749979
>>

I think the first one, 5.sqrt, looks much better than Math.sqrt(5).

Thanks,
Dan

Daniel Finnie wrote:

Why does everything have to be Math.<func>(num)? Isn't num.func more object oriented-ish?

That was one the first questions I had
when I came to the Ruby world.

FWIW, here's a related answer from Matz,

  http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/71386

Regards,

···

--
Bil Kleb
http://fun3d.larc.nasa.gov

Daniel Finnie [mailto:danfinnie@optonline.net] :
# >> (Math.methods - Object.methods).select {|x|
# Math.method(x).arity ==
# 1}.each do |x|
# ?> Numeric.class_eval { ?> define_method(x.to_sym) {
# Math.send(x.to_sym, self) } >> }
# >> end
# => ["tan", "frexp", "sinh", "exp", "acos", "tanh", "log", "asin",
# "acosh", "cos", "log10", "atan", "erf", "asinh", "sin",
# "sqrt", "cosh",
# "erfc", "atanh"]
# >> *5.sqrt*
# => 2.23606797749979
# >> *Math.sqrt(5)*
# => 2.23606797749979
# >>

···

#
# I think the first one, 5.sqrt, looks much better than Math.sqrt(5).

Indeed, but only on the first glance.

Math.sqrt *is the sine function; which in turn has its own set of properties/behavior among the gazillion of other functions found in mathematics. You pass only 5 here as the parameter. So it is not really object for objects sake. We have to bear in mind what object we want to center our minds on. Here, in this case, the object may be the sine behavior, and the behavior varies by changing the parameter provided. And hey, is 5 in degrees or in radians? Maybe that should be 5.radians.sine or sine(5.radians) --just joking :slight_smile:

kind regards -botp

ps: i do also use 5.sin though. sometimes, i feel it is not good to stick to traditions especially when i'm teaching my kids ruby. in programming, i do not want them to think that *it's the *only way (we write things).. Welcome to the brave new world of ruby.

# Thanks,
# Dan

Daniel Finnie wrote:

Why does everything have to be Math.<func>(num)? Isn't num.func more
object oriented-ish?

In addition to the answers by others, what would you do for those math
functions whose arity isn't 1?

  Math.methods(false).select{ |n| Math.method(n).arity != 1 }
  #=> ["hypot", "atan2", "ldexp"]

All three take two arguments. While 3.hypot(4) *might* make
sense--given one leg of a right triangle, how long is the hypotenuse to
a given other leg--30.atan2( 40 ) makes little sense to me.

A few exceptions might not make a good arguments against 19 possibly
plausible cases. In my opinion, however, once you have to break the
rule it makes sense to look for a new way to handle all the cases
similarly.

And, personally, I agree with Matz - the trigonometric functions simply
look more correct (to me) as functions that take an argument, not
methods invoked upon a number. That's purely a matter of taste, though.

Daniel Finnie wrote:

Why does everything have to be Math.<func>(num)? Isn't num.func more object oriented-ish?

Having the math functions in a module means there could be an alternate module with a different implementation.

Why another implementation of Math::sin?

Well, I notice that some folks on this thread have used the example of sin(90), probably expecting the argument units to be degrees rather than radians. There could be an alternate library using degrees:

MathDeg::sin(90) # ==> 1.0

Another example might be a version of Math that used something other than libm, perhaps something with different precision, or optimized differently.

The point is that the various math functions (or a particular implementation of them) belong together more than they belong to numbers.

···

--
        vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Why does everything have to be Math.<func>(num)? Isn't num.func more
object oriented-ish?

Also, i forgot to mention, by including the module, you can make
things look very similar to what you'd actually see in equations.

include Math

=> Object

3 * sin(40)

=> 2.23533948143805

···

On 1/8/07, Daniel Finnie <danfinnie@optonline.net> wrote:

Daniel Finnie wrote:

Why does everything have to be Math.<func>(num)? Isn't num.func more
object oriented-ish?

For example:

(Math.methods - Object.methods).select {|x| Math.method(x).arity ==

1}.each do |x|
?> Numeric.class_eval { ?> define_method(x.to_sym) {
Math.send(x.to_sym, self) } >> }

  end

=> ["tan", "frexp", "sinh", "exp", "acos", "tanh", "log", "asin",
"acosh", "cos", "log10", "atan", "erf", "asinh", "sin", "sqrt", "cosh",
"erfc", "atanh"]

*5.sqrt*

=> 2.23606797749979

*Math.sqrt(5)*

=> 2.23606797749979

  I have to say I'm very happy with that sort of things, as in a program
of my own, (plotting scientific data), the user just can say

ctioga --math 'sin(x)'

to plot the sine of x.

It would be confusing for a non rubyist to run

ctioga --math 'x.sin'

  Wouldn't it ? Don't forget in this particular case, syntax can have
impact on non-programming users... Cheers !

  Vince

···

--
Vincent Fourmond, PhD student
http://vincent.fourmond.neuf.fr/

I have to think this is the first time I've ever really disagreed with matz and don't really understand his logic.

If I were to ask someone for the length of the word apples, I would say "What is the length of apples?" In ruby, this would be "apples".length.

If I were to ask someone for the sine of, say, 90, I would say "What is the sine of 90?" In ruby, this would be Math.sin(90) even though the structure of the sentence is the same as for "apples" above.

Dan

Bil Kleb wrote:

···

Daniel Finnie wrote:

Why does everything have to be Math.<func>(num)? Isn't num.func more object oriented-ish?

That was one the first questions I had
when I came to the Ruby world.

FWIW, here's a related answer from Matz,

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/71386

Regards,
--
Bil Kleb
http://fun3d.larc.nasa.gov

hypot and atan are vector methods, so [30, 40].atan2, [3,4].hypot
would be correct in the OO-sense, much like #max and #min are in Array
and not Math (compare to e.g. JavaScript where there is Math.max and
Math.min)

···

On 1/9/07, Phrogz <gavin@refinery.com> wrote:

Daniel Finnie wrote:
> Why does everything have to be Math.<func>(num)? Isn't num.func more
> object oriented-ish?

In addition to the answers by others, what would you do for those math
functions whose arity isn't 1?

  Math.methods(false).select{ |n| Math.method(n).arity != 1 }
  #=> ["hypot", "atan2", "ldexp"]

All three take two arguments. While 3.hypot(4) *might* make
sense--given one leg of a right triangle, how long is the hypotenuse to
a given other leg--30.atan2( 40 ) makes little sense to me.

Bil Kleb <Bil.Kleb@NASA.gov> writes:

Daniel Finnie wrote:

Why does everything have to be Math.<func>(num)? Isn't num.func
more object oriented-ish?

That was one the first questions I had
when I came to the Ruby world.

FWIW, here's a related answer from Matz,

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/71386

Hmpf. In my math textbook 1/2 != 0, and there's no such thing as a
"zeroth" element. (I guess there is some complex tradition precidence
at work.)

:slight_smile:

Steve

···

Regards,
--
Bil Kleb
http://fun3d.larc.nasa.gov

but 90.sin * 10 looks as ugly as can be.

···

On 1/8/07, Daniel Finnie <danfinnie@optonline.net> wrote:

I have to think this is the first time I've ever really disagreed with
matz and don't really understand his logic.

If I were to ask someone for the length of the word apples, I would say
"What is the length of apples?" In ruby, this would be "apples".length.

If I were to ask someone for the sine of, say, 90, I would say "What is
the sine of 90?" In ruby, this would be Math.sin(90) even though the
structure of the sentence is the same as for "apples" above.

Hi,

I have to think this is the first time I've ever really disagreed with
matz and don't really understand his logic.

Probably it's highly influenced by the languages we speak.
Unfortunately, English is not the only language on Earth.

If I were to ask someone for the length of the word apples, I would say
"What is the length of apples?" In ruby, this would be "apples".length.

I would say "apples no nagasa wa?" in Japanese (nagasa = length).

If I were to ask someone for the sine of, say, 90, I would say "What is
the sine of 90?" In ruby, this would be Math.sin(90) even though the
structure of the sentence is the same as for "apples" above.

I would say "sin(90) wa?". See? It's different. :wink:

Besides that, and far more importantly, Ruby honors UNIX math library
(libm). All functions in Math module are found in libm.

              matz.

···

In message "Re: Why is there a seperate Math class?" on Tue, 9 Jan 2007 11:21:21 +0900, Daniel Finnie <danfinnie@optonline.net> writes:

If I were to ask someone for the length of the word apples, I
would say "What is the length of apples?" In ruby, this would
be "apples".length.

Length is a property _OF_ "apples".

If I were to ask someone for the sine of, say, 90, I would
say "What is the sine of 90?" In ruby, this would be
Math.sin(90)

Sin is an action _WITH_ 90.

even though the structure of the sentence is the same as for
"apples" above.

To me, it's not.

gegroet,
Erik V. - http://www.erikveen.dds.nl/

Hi,

···

In message "Re: Why is there a seperate Math class?" on Wed, 10 Jan 2007 07:52:15 +0900, Steven Lumos <steven@lumos.us> writes:

Hmpf. In my math textbook 1/2 != 0, and there's no such thing as a
"zeroth" element. (I guess there is some complex tradition precidence
at work.)

Yep, try require 'mathn'.

              matz.

Hi --

···

On Tue, 9 Jan 2007, Gregory Brown wrote:

On 1/8/07, Daniel Finnie <danfinnie@optonline.net> wrote:

I have to think this is the first time I've ever really disagreed with
matz and don't really understand his logic.

If I were to ask someone for the length of the word apples, I would say
"What is the length of apples?" In ruby, this would be "apples".length.

If I were to ask someone for the sine of, say, 90, I would say "What is
the sine of 90?" In ruby, this would be Math.sin(90) even though the
structure of the sentence is the same as for "apples" above.

but 90.sin * 10 looks as ugly as can be.

The expression you're looking for is "ugly as sin" :slight_smile:

David

--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black\)
    (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf\)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)

Erik Veenstra wrote:

> If I were to ask someone for the length of the word apples, I
> would say "What is the length of apples?" In ruby, this would
> be "apples".length.

Length is a property _OF_ "apples".

> If I were to ask someone for the sine of, say, 90, I would
> say "What is the sine of 90?" In ruby, this would be
> Math.sin(90)

Sin is an action _WITH_ 90.

Very crooked thinking. Consider

  %w( the ruby mafia strikes again ).sort

Is 'sort' a property of %w( the ruby mafia strikes again ) ?

> even though the structure of the sentence is the same as for
> "apples" above.

To me, it's not.

I'll help you out. Consider

  What is the zig of zag?
  What is the boo of hoo?

See the similarity? However, Ruby, a programming language, doesn't
need to emulate English.

"Erik Veenstra" <erikveen@gmail.com> writes:

If I were to ask someone for the length of the word apples, I
would say "What is the length of apples?" In ruby, this would
be "apples".length.

Length is a property _OF_ "apples".

If I were to ask someone for the sine of, say, 90, I would
say "What is the sine of 90?" In ruby, this would be
Math.sin(90)

Sin is an action _WITH_ 90.

even though the structure of the sentence is the same as for
"apples" above.

To me, it's not.

gegroet,
Erik V. - http://www.erikveen.dds.nl/

I tend to agree. Length can be interpreted as part of, or an attribute
of the object (string) apples. Sin is an action in a class of mathematical
operations which can operate on instances from a class of numbers.

Tim

···

--
tcross (at) rapttech dot com dot au

The way it's done now (sin(90), cos(90), etc.) makes sense because
it's similar to the actual mathemathical notation I learned back in
grade school. I find it easier to parse than 90.sin, especially when
the argument is actually an expression rather than a constant number.

···

On 1/9/07, William James <w_a_x_man@yahoo.com> wrote:

See the similarity? However, Ruby, a programming language, doesn't
need to emulate English.

--
Bira

http://sinfoniaferida.blogspot.com

Bira wrote, On 1/9/2007 5:36 AM:

···

On 1/9/07, William James <w_a_x_man@yahoo.com> wrote:

See the similarity? However, Ruby, a programming language, doesn't
need to emulate English.

The way it's done now (sin(90), cos(90), etc.) makes sense because
it's similar to the actual mathemathical notation I learned back in
grade school. I find it easier to parse than 90.sin, especially when
the argument is actually an expression rather than a constant number.

+1

Bira wrote:

The way it's done now (sin(90), cos(90), etc.) makes sense because
it's similar to the actual mathemathical notation I learned back in
grade school. I find it easier to parse than 90.sin, especially when
the argument is actually an expression rather than a constant number.

Generally I find that I often prefer functional style over OO whenever
the behavior of a method should only ever depend on basic properties of
the public API of an object rather than internal details, don't need
knowledge of the "real" class of an object, and don't have side
effects. Most numerical method would fall in that category.

There are inconsistencies that annoy me with Ruby, though, such as
Math.sin(x) but x.abs

Vidar