Programmering i Ruby

Den Pragmatiske Programmerers Veiledning

Forrige < Innhold ^
Neste >
klassen Float
Forelder: Numeric
Versjon: 1.6

Indeks:

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


Float objects represent real numbers using the native architecture's double-precision floating point representation.

instansmetoder
Arithmetic operations

Performs various arithmetic operations on flt.

flt + aNumeric Addition
flt -- aNumeric Subtraction
flt * aNumeric Multiplication
flt / aNumeric Division
flt % aNumeric Modulo
flt ** aNumeric Exponentiation

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

Returns -1, 0, or +1 depending on whether flt is less than, equal to, or greater than aNumeric. This is the basis for the tests in Comparable.

ceil flt.ceil -> anInteger

Returns the smallest Integer greater than or equal to flt.

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

finite? flt.finite? -> true or false

Returns true if flt is a valid IEEE floating point number (it is not infinite, and nan? is false).

floor flt.floor -> anInteger

Returns the largest integer less than or equal to flt.

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

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

Returns nil, -1, or +1 depending on whether flt is finite, -infinity, or +infinity.

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

nan? flt.nan? -> true or false

Returns true if flt is an invalid IEEE floating point number.

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

round flt.round -> anInteger

Rounds flt to the nearest integer. Equivalent to:

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

Returns flt.

to_i flt.to_i -> anInteger

Returns flt truncated to an Integer.

to_s flt.to_s -> aString

Returns a string containing a representation of self. As well as a fixed or exponential form of the number, the call may return ``NaN'', ``Infinity'', and ``-Infinity''.


Forrige < Innhold ^
Neste >

Extracted from the book "Programming Ruby - The Pragmatic Programmer's Guide".
Translation to norwegian by Norway Ruby User Group.
Copyright for the english original authored by David Thomas and Andrew Hunt:
Copyright © 2001 Addison Wesley Longman, Inc.
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/).

(Please note that the license for the original has changed from the above. The above is the license of the original version that was used as a foundation for the translation efforts.)

Copyright for the norwegian translation:
Copyright © 2002 Norway Ruby User Group.
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.