Was Yukihiro Matsumoto interested in Math?
-
I ask that question in light of the fact that there are very few basic math methods available in Ruby, related to the attached pic.
- no cube root function
- no sin function
- no cos function
- no tan function
Is anyone with superior ruby skills able to create these 4 functions for use within the SketchUp Ruby API ? I would be grateful!
-
2: http://ruby-doc.org/core-1.8.6/Math.html#method-c-sin
3: http://ruby-doc.org/core-1.8.6/Math.html#method-c-cos
4: http://ruby-doc.org/core-1.8.6/Math.html#method-c-tanAs for 1: a simple Google search found this thread with lots of example methods for cube root: http://www.ruby-forum.com/topic/191287
-
For question 1) it's actually the 'squareroot of three' not a 'cuberoot', so this applies http://ruby-doc.org/core-1.8.6/Math.html#method-c-sqrt
Math.sqrt(3)
returns
1.73205080756888
As tt says, there are a range of Math tools available, and making the 'missing ones' like your own cube-root [or any root] method is very easyx**(1/3.0)
returns the cuberoot of 'x' - so if x=27 you get 3.0 as the answer...
-
In addition to the standard functions that are always loaded into the
Math
module, you can load the extended functions into theMath
module, byrequire
'ing the library extension 'mathn.rb', ie:with a path to a full Ruby install's lib directory in the
$LOAD_PATH
array:
require('mathn.rb')
This math extension, in turn, loads:
'complex.rb'
'rational.rb'
'matrix.rb'Once loaded... there are many more functions, including
sqrt()
andrsqrt()
, arc and hyperbolic functions. It also creates some newNumeric
classes, such asPrime
,Rational
andComplex
, and adds a few methods to existing classes, such asInteger
.Refer to docs on:
-
Thank you gentlemen: much appreciated, and I promise not to do math past my bedtime again. (Unless I forget!)
Advertisement