Options

# Numeric class

• #### Index

• Numeric class
• Numeric class
by corbaque
Dec 2 2006

This is a locked, single-post thread from Creation Asylum. Archived here to prevent its loss.
No support is given. If you are the owner of the thread, please contact administration.

I've creat this script for simplify my codes, for exemple :
Code:
`angle = ((((Math.sqrt(x) * (i * Math::PI / 180)) * 180 / Math::PI) * 10000).to_i / 10000.0`

to
Code:
`angle = (x.sqrt * i.to_r).to_d.trunc_at(5)`

(It's not a quote, just an exemple)

Add this script before main, name it "Numeric" :
Code:
```#=================================== # Add at Numeric's class #--------------------------------------------------------------- # Creat by Corbaque 02 / 12 / 2006 #--------------------------------------------------------------- # => degre => radian # => radian => degree # => square root # => truncate at x after point # Directly on number # like this : ## 180.to_r #=> 3.14159265358979 ## 3.14159265358979.to_d #=> 180 ## 25.sqrt #=> 5 ## 120.2522.trunc_at(2) #=> 120.25 #-------------------------------------------------------------- # Version 1.1 #=================================== class Numeric   #-------------------------------------------------------------   # Degree to Radian   #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -   # Exemple :   # 165.to_r   # (256 * 526 * Math:tongue.gifI / 2546).to_r   # -141.2564.to_r   #-------------------------------------------------------------   def to_r     self * Math::PI / 180   end   #-------------------------------------------------------------   # Radian to Degree   #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -   # Exemple :   # 2.54669572.to_d   # (659 * Math:tongue.gifI / 1626).to_d   # -62.4545.to_d   #-------------------------------------------------------------   def to_d     self * 180 / Math::PI   end   #-------------------------------------------------------------   # Square root   #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -   # Exemple :   # 25.sqrt   # (36 + 42).sqrt   #-------------------------------------------------------------   def sqrt     self**0.5   end   #-------------------------------------------------------------   # Truncate at   #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -   # x = Numbers of figure after the point   #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -   # Exemple :   # 120.2522.trunc_at(2) #=> 120.25   # 120.2522.trunc_at(-2) #=> 100.0   #-------------------------------------------------------------   def trunc_at(x)     (self * (10.0 ** x)).to_i / (10.0 ** x)   end end```

Think at read comments
Thanks given by: