Module: Roebe::Math
- Extended by:
- Math
- Included in:
- Math
- Defined in:
- lib/roebe/math/pi.rb,
lib/roebe/math/fact.rb,
lib/roebe/math/math.rb,
lib/roebe/math/circle.rb,
lib/roebe/math/sphere.rb,
lib/roebe/math/zylinder.rb
Overview
Roebe::Math
Defined Under Namespace
Modules: Zylinder Classes: Fact, Sphere
Constant Summary collapse
- PI =
#
PI
#
::Math::PI
- FORMULA_FOR_THE_CIRCUMFERENCE_OF_A_CIRCLE =
#
Roebe::Math::FORMULA_FOR_THE_CIRCUMFERENCE_OF_A_CIRCLE
#
'2 * π * r'
- FORMULA_FOR_THE_AREA_OF_A_CIRCLE =
#
Roebe::Math::FORMULA_FOR_THE_AREA_OF_A_CIRCLE
#
'π * r²'
Class Method Summary collapse
-
.mittelwert(*i) ⇒ Object
# === Roebe::Math.mittelwert.
-
.sqr(i) ⇒ Object
# === Math.sqr ========================================================================= #.
-
.sqrt3(i) ⇒ Object
# === Math.sqrt3.
-
.variance(i) ⇒ Object
# === Roebe::Math.variance.
Instance Method Summary collapse
-
#berechne_volumen(l, b, h, be_verbose = true) ⇒ Object
# === berechne_volumen.
-
#calculate_bmi(weight = 80, height = 185) ⇒ Object
# === calculate_bmi.
-
#calculate_volume_of_zylinder(r, h) ⇒ Object
# === calculate_volume_of_zylinder.
-
#dritte_wurzel_von(i) ⇒ Object
# === dritte_wurzel_von.
-
#fib(n) ⇒ Object
(also: #fibonacci)
# === fib.
-
#harmonisches_mittel(*args) ⇒ Object
# === harmonisches_mittel.
-
#is_prime(n) ⇒ Object
(also: #is_prime?)
# === is_prime.
-
#kreisflaeche(radius = 10, be_verbose = false) ⇒ Object
(also: #circular_surface, #kreisfläche, #flächeninhalt)
# === kreisflaeche.
-
#kreisumfang(radius = 10, be_verbose = false) ⇒ Object
(also: #umfang_des_kreises, #circumference, #calculate_oberflaeche, #calculate_oberfläche)
# === kreisumfang.
-
#log(value = 10) ⇒ Object
# === log.
-
#pi ⇒ Object
# === pi ========================================================================= #.
-
#pythagoras(a, b) ⇒ Object
# === pythagoras.
-
#quadrat(seitenlaenge = 5) ⇒ Object
# === quadrat.
-
#sqr(i) ⇒ Object
(also: #sqrt, #square)
# === sqr ========================================================================= #.
-
#vierte_wurzel_von(i) ⇒ Object
# === vierte_wurzel_von ========================================================================= #.
Class Method Details
.mittelwert(*i) ⇒ Object
37 38 39 40 41 42 43 44 |
# File 'lib/roebe/math/math.rb', line 37 def self.mittelwert(*i) i.flatten! if i.first.is_a?(String) i.map! {|entry| entry.to_f } end result = i.sum / i.size.to_f return result.round(2) end |
.sqr(i) ⇒ Object
#
Math.sqr
#
245 246 247 |
# File 'lib/roebe/math/math.rb', line 245 def self.sqr(i) sqrt(i) end |
.sqrt3(i) ⇒ Object
222 223 224 |
# File 'lib/roebe/math/math.rb', line 222 def Math.sqrt3(i) return i.to_f ** (1 / 3.0) end |
.variance(i) ⇒ Object
52 53 54 55 56 57 |
# File 'lib/roebe/math/math.rb', line 52 def self.variance(i) m = mittelwert(i) sum = 0.0 i.each {|v| sum += (v-m)**2 } sum / i.size end |
Instance Method Details
#berechne_volumen(l, b, h, be_verbose = true) ⇒ Object
#
berechne_volumen
The arguments to this method are:
l Länge
b Breite
h Höhe
#
185 186 187 188 189 190 191 192 193 |
# File 'lib/roebe/math/math.rb', line 185 def berechne_volumen(l, b, h, be_verbose = true) volumen = l * b * h if be_verbose puts 'Das Volumen von Länge: '+l.to_s+ ', Breite: '+b.to_s+ ", Höhe: #{h} ist: #{volumen}" end return volumen end |
#calculate_bmi(weight = 80, height = 185) ⇒ Object
#
calculate_bmi
Calculates the BMI index of human beings.
Bmi is:
weight / (height * height)
in kg and metres.
We pass the height in cm though.
#
99 100 101 102 |
# File 'lib/roebe/math/math.rb', line 99 def calculate_bmi(weight = 80, height = 185) h = (height/100.to_f).to_f return ( weight / ( (h) * (h) ) ) end |
#calculate_volume_of_zylinder(r, h) ⇒ Object
#
calculate_volume_of_zylinder
This allows you to calculate the Volume of a Zylinder. We require two arguments as input - radius and height.
Usage example:
Math.calculate_volume_of_zylinder(10, 10)
#
237 238 239 240 |
# File 'lib/roebe/math/math.rb', line 237 def calculate_volume_of_zylinder(r, h) _ = Zylinder.calculate_volume(r, h) return _ end |
#dritte_wurzel_von(i) ⇒ Object
#
dritte_wurzel_von
Es ist wohl besser die Methode unten zu verwenden.
Math.sqrt3()
#
209 210 211 |
# File 'lib/roebe/math/math.rb', line 209 def dritte_wurzel_von(i) return i.to_f ** (1 / 3.0) end |
#fib(n) ⇒ Object Also known as: fibonacci
#
fib
The fibonacci in Ruby.
#
150 151 152 153 154 155 156 |
# File 'lib/roebe/math/math.rb', line 150 def fib(n) n = n.to_i return n if n < 2 vals = [0, 1] n.times { vals.push(vals[-1] + vals[-2]) } return vals.last end |
#harmonisches_mittel(*args) ⇒ Object
#
harmonisches_mittel
URL:
http://de.wikipedia.org/wiki/Harmonisches_Mittel
Das harmonische Mittel ermittelt man, indem man n durch die Summe der Kehrwerte der Merkmalsbeträge dividiert, wobei n die Anzahl der Merkmalsträger ist.
Ein praktisches Beispiel:
Ein Autofahrer legt 60 km mit 30 km/h, die nächsten 60 km mit 60 km/h
zurück. Wie hoch war die Durchschnittsgeschwindigkeit?
Hier darf man nicht einfach addieren. Wir überlegen: Der Autofahrer hat für die ersten 60 km 2 h, für die zweiten 60 km eine Stunde gebraucht, insgesamt also 3 h für 120 km. Das ergibt eine Durchschnittsgeschwindigkeit von 40 km/h.
Definition: Das harmonische Mittel zweier Zahlen a und b ist die Zahl h, für die gilt:
(result - a) : (b - result) = a : b
Die Abstände von h zu den Zahlen a und b verhalten sich so wie die ursprünglichen Zahlen (ist b doppelt so groß wie a, so ist h von b doppelt so weit entfernt wie von a
Das harmonische Mittel h zweier von Null verschiedener reeller Zahlen a und b ist Zwei geteilt durch die Summe ihrer Kehrwerte.
#
137 138 139 140 141 142 143 |
# File 'lib/roebe/math/math.rb', line 137 def harmonisches_mittel(*args) n_elements = Float(args.size) divisor = 0.0 args.each { |argument| divisor += (1.0 / Float(argument) ) } result = ( n_elements / divisor ) return result end |
#is_prime(n) ⇒ Object Also known as: is_prime?
84 85 86 |
# File 'lib/roebe/math/math.rb', line 84 def is_prime(n) ('1' * n) !~ /^1?$|^(11+?)\1+$/ end |
#kreisflaeche(radius = 10, be_verbose = false) ⇒ Object Also known as: circular_surface, kreisfläche, flächeninhalt
#
kreisflaeche
Die Formel für die Kreisfläche ist: A = r² * PI
#
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/roebe/math/circle.rb', line 59 def kreisflaeche( radius = 10, be_verbose = false ) if radius.is_a? Hash # ===================================================================== # # === :radius # ===================================================================== # if radius.has_key? :radius radius = radius.delete(:radius) end end area = (radius.to_f ** 2) * PI area = '%.3f' % area # Round it up to 4 decimal places for accuracy puts "The Area of the circle with a radius "+ "of #{radius} is #{area}." if be_verbose return area end |
#kreisumfang(radius = 10, be_verbose = false) ⇒ Object Also known as: umfang_des_kreises, circumference, calculate_oberflaeche, calculate_oberfläche
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/roebe/math/circle.rb', line 32 def kreisumfang( radius = 10, be_verbose = false ) if radius.is_a? Hash # ===================================================================== # # === :radius # ===================================================================== # if radius.has_key? :radius radius = radius.delete(:radius) end end umfang = 2 * radius.to_f * PI umfang = '%.3f' % umfang # Round it up to 4 decimal places for accuracy. Don't need to many Kommastellen. puts "The Circumference of a circle with a radius "+ "of #{radius} is #{umfang}." if be_verbose return umfang end |
#log(value = 10) ⇒ Object
71 72 73 |
# File 'lib/roebe/math/math.rb', line 71 def log(value = 10) Math.log10(value) end |
#pythagoras(a, b) ⇒ Object
267 268 269 270 271 272 273 274 275 276 277 278 279 |
# File 'lib/roebe/math/math.rb', line 267 def pythagoras(a, b) if a.is_a? Array if a.size > 1 _ = a a = _[0] b = _[1] end end a = a.to_f b = b.to_f c = Math.sqrt(a ** 2 + b ** 2) # Die Seite c. return c end |
#quadrat(seitenlaenge = 5) ⇒ Object
#
quadrat
This bundles together all Quadrat-actions (these are square-actions).
A Quadrat ist “ein Rechteck mit gleicher Seitenlänge.
Usage example:
Math.quadrat 10
#
170 171 172 173 |
# File 'lib/roebe/math/math.rb', line 170 def quadrat(seitenlaenge = 5) require 'x/MATH/quadrat.rb' # bl $RSRC/MATH/quadrat.rb Quadrat.new(seitenlaenge) end |
#sqr(i) ⇒ Object Also known as: sqrt, square
#
sqr
#
252 253 254 |
# File 'lib/roebe/math/math.rb', line 252 def sqr(i) Math.sqr(i) end |
#vierte_wurzel_von(i) ⇒ Object
#
vierte_wurzel_von
#
198 199 200 |
# File 'lib/roebe/math/math.rb', line 198 def vierte_wurzel_von(i) return i.to_f ** (1 / 4.0) end |