Programmierung in Ruby

Der Leitfaden der Pragmatischen Programmierer

class Float
Parent: Numeric
Version: 1.6

Index:

Arithmetic operations <=> ceil finite? floor infinite? nan? round to_f to_i to_s


Float Objekte repräsentieren reelle Zahlen (Gleitkommazahlen) unter Verwendung der Architektur-spezifischen Darstellung von "double-precision floating point" Zahlen, also Gleitkommazahlen mit doppelter Genauigkeit.

instance methods
Arithmetic operations

Führt verschiedene arithmetische Operationen auf flt aus.

flt + eineZahl Addition
flt -- eineZahl Subtraktion
flt * eineZahl Multiplikation
flt / eineZahl Division
flt % eineZahl Modulo
flt ** eineZahl Potenzierung

<=> flt <=> eineZahl-> -1, 0, +1

Gibt abhängig davon, ob flt kleiner, gleich oder größer als einZahl ist, -1, 0, oder +1 zurück. Dies ist die Basis für die Tests in Comparable.

ceil flt.ceil -> einInteger

Gibt die kleinste Ganzzahl zurück, die größer oder gleich zu flt ist.

1.2.ceil » 2
2.0.ceil » 2
(-1.2).ceil » -1
(-2.0).ceil » -2

finite? flt.finite? -> true oder false

Gibt true zurück, wenn flt eine gültige IEEE Gleitkommazahl ist (d.h. sie ist nicht unendlich, und nan? ergibt false).

floor flt.floor -> einInteger

Gibt die größte Ganzzahl zurück, die kleiner oder gleich zu flt ist.

1.2.floor » 1
2.0.floor » 2
(-1.2).floor » -2
(-2.0).floor » -2

infinite? flt.infinite? -> nil, -1, +1

Gibt abhängig davon ,ob flt endlich, -unendlich oder +unendlich ist, nil, -1, bzw. +1 zurück.

(0.0).infinite? » nil
(-1.0/0.0).infinite? » -1
(+1.0/0.0).infinite? » 1

nan? flt.nan? -> true oder false

Gibt true zurück, wenn flt eine ungültige IEEE Gleitkommazahl ist.

a = -1.0 » -1.0
a.nan? » false
a = Math.log(a) » NaN
a.nan? » true

round flt.round -> einInteger

Rundet flt auf die nächstgelegene Ganzzahl. Equivalent mit:

def round
  return floor(self+0.5) if self > 0.0
  return ceil(self-0.5)  if self < 0.0
  return 0.0
end

1.5.round » 2
(-1.5).round » -2

to_f flt.to_f -> flt

Gibt flt zurück.

to_i flt.to_i -> einInteger

Gibt flt als Ganzzahl mit abgeschnittenem Nachkommateil zurück.

to_s flt.to_s -> einString

Gibt einen String mit der Darstellung von self zurück. Ebenso wie die Darstellung in der Dezimal- oder Exponentialform kann der Methodenaufruf ``NaN'', ``Infinity'', und ``-Infinity'' für ungültige bzw. positiv oder negativ unendliche Zahlen zurückgeben.


Extracted from the book "Programming Ruby - The Pragmatic Programmer's Guide"
Übersetzung: Carsten Schinzer
Für das englische Original:
© 2000 Addison Wesley Longman, Inc. Released under the terms of the Open Publication License V1.0. That reference is available for download.
Diese Lizenz sowie das Original vom Herbst 2001 bilden die Grundlage der Übersetzung
Es wird darauf hingewiesen, dass sich die Lizenz des englischen Originals inzwischen geändert hat.
Für die deutsche Übersetzung:
© 2002 Jürgen Katins
Der Copyright-Eigner stellt folgende Lizenzen zur Verfügung:
Nicht-freie Lizenz:
This material may be distributed only subject to the terms and conditions set forth in the Open Publication License, v1.0 or later (the latest version is presently available at http://www.opencontent.org/openpub/). Distribution of substantively modified versions of this document is prohibited without the explicit permission of the copyright holder. Distribution of the work or derivative of the work in any standard (paper) book form is prohibited unless prior permission is obtained from the copyright holder.
Freie Lizenz:
Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with no Invariant Sections, with no Front-Cover Texts, and with no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License".