红宝石 |浮动类
在 Ruby 中,Float 类是 Numeric 类的子类。 Float 类的对象使用本机架构的双精度浮点表示来表示实数。
公共实例方法
- 算术运算:此方法对float执行各种算术运算。
- 加法:返回浮点数与浮点数之和的结果。
float + numeric
- 减法:返回浮点数中浮点数与数值的差值。
float - numeric
- 乘法:返回浮点数与数值乘积的结果。
float * numeric
- 除法:返回浮点数与浮点数的数值相除的结果。
float / numeric
- 取模:返回浮点数与数值取模的结果。
float % numeric
- 指数:返回浮点数和数值的幂的结果。
float ** numeric
- 一元减:返回浮点数。
float -@
例子:
# Ruby program to illustrate # Arithmetic operation a = 2.1 b = 2 # Addition c = a + b puts "addition #{c}" # Subtraction d = a - b puts "subtraction #{d}" # Multiplication e = a * b puts "multiplication #{e}" # Division f = a / b puts "division #{f}" # Modulo g = a % b puts "modulo #{g}" # Exponent h = a ** b puts "exponent #{h}" # Unary minus i= -a puts "unary minus #{i}"
输出:
addition 4.1 subtraction 0.1 multiplication 4.2 division 1.05 modulo 0.1 exponent 4.41 unary minus -2.1
- 加法:返回浮点数与浮点数之和的结果。
- <=> :此方法返回 -1、0 或 +1 取决于float 。如果float小于数值则返回-1,如果float等于数值则返回0,或者如果float大于数值则返回+1。
float <=> numeric --> 1, 0, +1
例子:
# Ruby program to illustrate # <=> Method puts 2.1 <=> 4 puts 2.0 <=> 2 puts 4.6 <=> 2
输出:
-1 0 1
- == :如果obj等于float ,则此方法返回 true,否则返回 false。
float == obj --> true or false
例子:
# Ruby program to illustrate # == Method puts 3.8 == 4 puts 3.8 == 3.8
输出:
false true
- abs :此方法返回float的绝对值。
float.abs --> numeric
例子:
# Ruby program to illustrate # abs Method puts (-54.56).abs puts (-65.04).abs
输出:
54.56 65.04
- ceil :此方法返回大于或等于float的最小 Integer。此方法的返回类型是 int。
float.ceil --> int
例子:
# Ruby program to illustrate # ceil Method puts (4.1).ceil puts (4.0).ceil puts (-4.1).ceil
输出:
5 4 -4
- divmod :此方法将返回一个数组,其中包含通过将 num 除以数字获得的商和模数。
float.divmod(numeric) --> array
例子:
# Ruby program to illustrate # divmod Method p (45.0.divmod 5) p (98.0.divmod 5)
输出:
[9, 0.0] [19, 3.0]
- 情商? :此方法检查obj是否为 Float 并且包含与float中相同的值。如果它们包含相同的值,那么它将返回 true,否则返回 false。此方法的返回类型是boolean 。
float.eql?(obj) --> true or false
例子:
# Ruby program to illustrate # eql? Method puts 4.2.eql?(2) puts 1.2.eql?(1.2)
输出:
false true
- 有限? :此方法检查浮点数是否为有效的 IEEE 浮点数。如果float是有效的 IEEE 浮点数,则返回 true,否则返回 false。
float.finite? --> true or false
例子:
# Ruby program to illustrate # finite? Method puts (45.0).finite? puts (45.0/0.0).finite?
输出:
true false
- floor :此方法返回小于或等于float的最大整数。
float.floor --> int
例子:
# Ruby program to illustrate # floor Method puts 2.2. floor puts (-4.6).floor
输出:
2 -5
- 无限的? :此方法返回nil、-1 或 +1 ,它取决于float 。如果float是有限的,那么它返回nil ,如果float是 -infinite ,那么它返回-1 ,或者如果float是 +infinite ,那么它返回+1 。
float.infinite? --> nil, -1, +1
例子:
# Ruby program to illustrate # infinite? Method puts (1.1).infinite? puts (-1.1/0.0).infinite? puts (+1.1/0.0).infinite?
输出:
nil -1 1
- modulo:此方法类似于 Float#% 方法。
float.modulo(numeric) --> numeric
例子:
# Ruby program to illustrate # modulo Method puts 32.45.modulo(20)
输出:
12.450000000000003
- 楠? :如果float是无效的 IEEE 浮点数,则此方法返回 true,否则返回 false。此方法的返回类型是boolean 。
float.nan? --> true or false
例子:
# Ruby program to illustrate # nan? Method puts (-2.2). nan? puts (0.0/0.0). nan?
输出:
false true
- round:此方法将浮点数四舍五入到最接近的整数值。此方法的返回类型是int 。
float..round(digits=0) --> numeri
例子:
# Ruby program to illustrate # round Method puts 6.7.round puts (-8.9).round
输出:
7 -9
- to_f :此方法返回float 。
float.to_f --> float
- to_i :此方法将浮点截断返回为整数。此方法的返回类型是int 。
float.to_i --> int
例子:
# Ruby program to illustrate # to_i Method puts 5.6.to_i
输出:
5
- to_int :此方法类似于 Float#to_i。
float.to_int --> int
- to_s :此方法返回一个包含自我表示的字符串,以及固定或指数形式的编号。该调用可能返回 NaN、infinity 和 -infinity。
float.to_s --> string
- truncate :此方法等同于 Float#to_i 方法。此方法的返回类型是 int。
float.truncate
- 零? : 如果float为 0.0,此方法返回 true,否则返回 false。此方法的返回类型是布尔值。
float.zero? --> true or false
例子:
# Ruby program to illustrate # zero? Method puts (0.0).zero? puts (1.4).zero?
输出:
true false
Float 类包含如下所列的常量:
Constants Description DIG It holds minimum number of significant decimal digits in a double-precision floating point and it defaults to 15. EPSILON It holds difference between 1 and the smallest double-precision floating point number greater than 1 and defaults to 2.2204460492503131e-16. MANT_DIG It holds the number of mantissa digits of base RADIX. Defaults to 53. MAX It holds largest possible integer in a double-precision floating point number and it defaults to 1.7976931348623157e+308. MAX_10_EXP It represent the largest positive exponent in a double-precision floating point where 10 raised to this power minus 1. Defaults to 308. MAX_EXP It is the largest possible exponent value in a double-precision floating point which defaults to 1024. MIN It is the smallest positive normalized number in a double-precision floating point. Defaults to 2.2250738585072014e-308. MIN_10_EXP It is the smallest negative exponent in a double-precision floating point where 10 raised to this power minus 1. Defaults to -307. MIN_EXP It is the smallest possible exponent value in a double-precision floating point. Defaults to -1021 RADIX The radix of floating-point representations or in other words, it is a base of floating-point numbers. Defaults to 2 on most systems, which would represent a base-10 decimal ROUND It represents the rounding mode for floating-point operations. The values includes are:
-1: if the mode is indeterminate
0: if rounding towards zero
1: if the rounding is nearest to representable value
2: if rounding is towards +infinite
3: if rounding is towards +infiniteNaN It is an expression representing a value which is “not a number“. INFINITY It is an expression representing positive infinity. 参考: https://ruby-doc.org/core-2.4.0/Float.html