Why is there a seperate Math class?

D

Daniel Finnie

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

For example:1}.each do |x|
?> Numeric.class_eval { ?> define_method(x.to_sym) {
Math.send(x.to_sym, self) } >> }=> ["tan", "frexp", "sinh", "exp", "acos", "tanh", "log", "asin",
"acosh", "cos", "log10", "atan", "erf", "asinh", "sin", "sqrt", "cosh",
"erfc", "atanh"]
I think the first one, 5.sqrt, looks much better than Math.sqrt(5).

Thanks,
Dan
 
D

Daniel Finnie

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
 
G

Gregory Brown

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.
 
P

Phrogz

Daniel said:
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.
 
E

Erik Veenstra

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/
 
W

William James

Erik said:
Length is a property _OF_ "apples".


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 ) ?
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.
 
B

Bira

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.
 
I

Ilmari Heikkinen

Daniel said:
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.

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)
 
S

Sammy Larbi

Bira wrote, On 1/9/2007 5:36 AM:
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
 
J

Joel VanderWerf

Daniel said:
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.
 
G

Gregory Brown

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.
=> 2.23533948143805
 
V

Vincent Fourmond

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

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

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
 
M

Maël Clérambault

Let me try to make everybody happy :

class Numeric
Math.methods(false).each { |m|
sm =3D Math.method(m)
if Math.method(m).arity =3D=3D 1
define_method m do=20
sm.call self
end
end
}
end

irb(main):001:0> require 'math_extend'
=3D> true
irb(main):002:0> 34.cos
=3D> -0.848570274784605
irb(main):003:0> Math.cos 34
=3D> -0.848570274784605

Regards,
Ma=EBl Cl=E9rambault
 
V

Vidar Hokstad

Bira said:
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
 
G

Gregory Brown

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

x.abs seems to me a convenience method.

If you're using irb in math mode or something like this, this might
make your functions look prettier:
=> 10.2114125
 

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

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top