Ruby 2.1.0 リファレンスマニュアル > ライブラリ一覧 > 組み込みライブラリ > Floatクラス

class Float

クラス・モジュールの継承リスト: Float < Numeric < Comparable < Object < Kernel < BasicObject
dynamic include: JSON::Generator::GeneratorMethods::Float (by json)

要約

浮動小数点数のクラス。Float の実装は C 言語の double で、その精度は環境に依存します。

一般にはせいぜい15桁です。詳しくは多くのシステムで採用されている浮動小数点標準規格、IEEE (Institute of Electrical and Electronics Engineers: 米国電気電子技術者協会) 754 を参照してください。

あるシステムでの 1/3(=0.333...) の結果

printf("%.50f\n", 1.0/3)
  # => 0.33333333333333331482961625624739099293947219848633

Math::PI などの数学定数については Math を参照してください。

インスタンスメソッド

定義 説明
self % other -> Float
modulo(other) -> Float

算術演算子。剰余を計算します。

self * other -> Float

算術演算子。積を計算します。

self ** other -> Float

算術演算子。冪を計算します。

self + other -> Float

算術演算子。和を計算します。

self - other -> Float

算術演算子。差を計算します。

- self -> Float

単項演算子の - です。 self の符号を反転させたものを返します。

self / other -> Float

算術演算子。商を計算します。

self < other -> bool

比較演算子。数値として小さいか判定します。

self <= other -> bool

比較演算子。数値として等しいまたは小さいか判定します。

self <=> other -> -1 | 0 | 1 | nil

self と other を比較して、self が大きい時に正、等しい時に 0、小さい時に負の整数を返します。比較できない場合はnilを返します

self == other -> bool

比較演算子。数値として等しいか判定します。

self > other -> bool

比較演算子。数値として大きいか判定します。

self >= other -> bool

比較演算子。数値として等しいまたは大きいか判定します。

abs -> Float
magnitude -> Float

自身の絶対値を返します。

arg -> 0 | Float
angle -> 0 | Float
phase -> 0 | Float

自身の偏角(正の数なら 0、負の数なら Math::PI)を返します。

ceil -> Integer

自身と等しいかより大きな整数のうち最小のものを返します。

denominator -> Integer

自身を Rational に変換した時の分母を返します。

divmod(other) -> [Numeric]

self を other で割った商 q と余り r を、 [q, r] という 2 要素の配列にして返します。商 q は常に整数ですが、余り r は整数であるとは限りません。

eql?(other) -> bool

自身と other のクラスが等しくかつ == メソッドで比較して等しい場合に true を返します。そうでない場合に false を返します。

finite? -> bool

数値が ∞, -∞, あるいは NaN でない場合に true を返します。そうでない場合に false を返します。

floor -> Integer

自身と等しいかより小さな整数のうち最大のものを返します。

hash -> Integer

ハッシュ値を返します。

infinite? -> 1 | -1 | nil

数値が +∞ のとき 1、-∞のとき -1 を返します。それ以外は nil を返します。

to_s -> String
inspect -> String

自身を人間が読みやすい形の文字列表現にして返します。

nan? -> bool

数値が NaN(Not a number)のとき真を返します。

numerator -> Integer

自身を Rational に変換した時の分子を返します。

rationalize -> Rational
rationalize(eps) -> Rational

自身から eps で指定した許容誤差の範囲に収まるような Rational を返します。

round(ndigits = 0) -> Integer | Float

自身ともっとも近い整数もしくは実数を返します。

to_f -> self

self を返します。

to_i -> Integer
truncate -> Integer

小数点以下を切り捨てて値を整数に変換します。

to_r -> Rational

自身を Rational に変換します。

zero? -> bool

自身がゼロの時、trueを返します。そうでない場合は false を返します。

定数

定義 説明
DIG -> Integer

Float が表現できる最大の 10 進桁数です。

EPSILON -> Float

1.0 + Float::EPSILON != 1.0 となる最小の正の値です。

INFINITY -> Float

浮動小数点数における正の無限大です。

MANT_DIG -> Integer

仮数部の Float::RADIX 進法での桁数です。

MAX -> Float

Float が取り得る最大の有限の値です。

MAX_10_EXP -> Integer

最大の 10 進の指数です。

MAX_EXP -> Integer

最大の Float::RADIX 進の指数です。

MIN -> Float

Float が取り得る最小の正の値です。

MIN_10_EXP -> Integer

最小の 10 進の指数です。

MIN_EXP -> Integer

最小の Float::RADIX 進の指数です。

NAN -> Float

浮動小数点数における NaN(Not a number)です。

RADIX -> Integer

指数表現の基数です。

ROUNDS -> Integer

この定数は Ruby 2.7 から deprecated です。使わないでください。

継承したメソッド

! != __id__ __send__ instance_eval instance_exec method_missing singleton_method_added singleton_method_removed singleton_method_undefined +@ abs2 coerce conj div fdiv i imag integer? nonzero? polar quo real real? rect remainder step to_c to_int !~ === =~ _dump class clone define_singleton_method display enum_for equal? extend freeze frozen? initialize initialize_copy instance_of? instance_variable_defined? instance_variable_get instance_variable_set instance_variables is_a? itself marshal_dump marshal_load method methods must_be must_be_close_to must_be_empty must_be_instance_of must_be_kind_of must_be_nil must_be_same_as must_be_within_epsilon must_equal must_include must_match must_raise must_respond_to must_send must_throw nil? object_id pretty_inspect pretty_print pretty_print_cycle pretty_print_inspect pretty_print_instance_variables private_methods protected_methods psych_to_yaml public_method public_methods public_send remove_instance_variable respond_to? respond_to_missing? send singleton_class singleton_method singleton_methods taint tainted? tap to_a to_ary to_hash to_io to_proc to_regexp to_str trust untaint untrust untrusted? .new .yaml_tag ::ARGF ::ARGV ::DATA ::ENV ::FALSE ::NIL ::RUBY_COPYRIGHT ::RUBY_DESCRIPTION ::RUBY_ENGINE ::RUBY_PATCHLEVEL ::RUBY_PLATFORM ::RUBY_RELEASE_DATE ::RUBY_REVISION ::RUBY_VERSION ::SCRIPT_LINES__ ::STDERR ::STDIN ::STDOUT ::TOPLEVEL_BINDING ::TRUE

追加されるメソッド

定義 説明 ライブラリ
to_d -> BigDecimal
to_d(prec) -> BigDecimal

自身を BigDecimal に変換します。

bigdecimal/util