Ruby 数字

在 Ruby 中,数字是可以用作数学运算的数值。

这里有一个 Ruby 数字的快速示例。你可以阅读其余教程以了解更多信息。

示例

puts 5           # Output: 5   
puts 5.55        # Output: 5.55

puts 5 + 5.55    # Output: 10.55

在这里,55.55 是数字,我们可以对它们执行算术运算,例如加法。


Ruby 数字类型

以下数据类型被视为 Ruby 中的数字

数据类型 描述 示例
整数 没有小数点的整数。 5-231024 等。
浮点数 带小数点的数字。 5.0-23.851024.369 等。
有理数 有理数,即分数,如 2/34/5 Rational(2, 3)Rational(4, 5) 等。
复数 具有实部和虚部的复数,例如 2 + 3i Complex(2, 3)Complex(4, 5) 等。
BigDecimal 非常大或非常精确的十进制数。 BigDecimal("0.123456789123456789")

在这里,IntegerFloat 是基本数字类型,而其他是高级类型。


Ruby 整数

整数是没有小数点的数字,例如 20-170300 等。例如,

# Create and print an integer variable
marks = 85
puts marks

# Output: 85

在这里,marks 是一个值为 85 的整数变量。


Ruby 浮点数

浮点数是带有小数点的数字,例如 20.25-170.39300.0 等。例如,

# Create and print a float variable
price = 99.99
puts price

# Output: 99.99

在这里,price 是一个值为 99.99 的浮点数变量。


数字的算术运算

您可以在算术运算中使用数字。例如,

# Arithmetic Operations on Integers

puts 9 + 3    # Output: 12
puts 9 - 3    # Output: 6
puts 9 * 3    # Output: 27
puts 9 / 3    # Output: 3
puts 9 % 3    # Output: 0

# Arithmetic Operations on Floats

puts 6.9 + 2.5    # Output: 9.4
puts 6.9 - 2.5    # Output: 4.4
puts 6.9 * 2.5    # Output: 17.25
puts 6.9 / 2.5    # Output: 2.7600000000000002
puts 6.9 % 2.5    # Output: 1.9000000000000004

更多关于 Ruby 整数和浮点数

浮点数的精度误差

考虑以下代码

puts 6.9 / 2.5    # Output: 2.7600000000000002
puts 6.9 % 2.5    # Output: 1.9000000000000004

请注意

  • 6.9 / 2.5 的结果是 2.7600000000000002,而不是 2.76
  • 6.9 % 2.5 的结果是 1.9000000000000004,而不是 1.9

这是因为计算机使用二进制数(基数为 2 的数)表示小数。

然而,某些小数不能在二进制中精确表示。

在这种情况下,Ruby 存储了该数字的近似值,这会导致计算过程中出现微小的精度误差。例如,

puts 0.1 + 0.2

# Output: 0.30000000000000004

虽然这些微小的差异通常不是问题,但在以下情况您应该注意它们:

  • 比较浮点数。例如,即使 a == b 看起来相等,它也可能返回 false
  • 处理金钱或科学值,在这种情况下您应该使用 BigDecimal 来获得精度。
整数除法与浮点数除法

对两个整数进行除法运算的结果将始终是整数结果。例如,

puts 9 / 4

# Output: 2

在这里,94 都是整数。因此 9 / 4 的结果将是整数值 2,而不是实际结果 2.25

要解决此问题,您必须在运算中使用至少一个浮点数。

或者,您可以使用 fdiv 方法进行浮点数除法。

注意: fdiv 仅从 Ruby 2.4 开始可用。

示例

# Divide 9.0 (float) by 4 (integer)
puts 9.0 / 4

# Divide 9 (integer) by 4.0 (float)
puts 9 / 4.0

# Divide 9.0 by 4.0
puts 9.0 / 4.0

# Use fdiv method (valid only for Ruby 2.4 and later)
puts 9.fdiv(4)

输出

2.25
2.25
2.25
2.25

记住

  • 整数 ÷ 整数 = 整数 (舍去小数部分)
  • 浮点数 ÷ 任何数 = 浮点数 (保留小数部分)
Ruby 中的整数除法截断

整数除法还有另一个有趣的特性。考虑以下程序

puts 7 / 2     # Output: 3
puts -7 / 2    # Output: -4
puts 7 / -2    # Output: -4
puts -7 / -2   # Output: 3

这里,

  • -7 / 27 / -2 的结果是 -4
  • 7 / 2-7 / -2 的结果是 3

这是因为 Ruby 在整数除法时总是向下取整,趋向于负无穷大。这被称为地板除法

所以,我们得到 -4 是因为它比 -3 更接近负无穷大。同样,3 比 4 更接近负无穷大。


Ruby 数字方法

Ruby 数字有许多方便的方法供您使用。一些常用的方法是

方法 描述
even? 检查整数是否为偶数。
odd? 检查整数是否为奇数。
ceil 向上舍入浮点数。
floor 向下舍入浮点数。
round 将浮点数舍入到最接近的整数。
next 下一个连续整数。
pred 上一个连续整数。
to_s 将数字转换为字符串。
gcd(x) x 的最大公约数。
n.times 将代码块重复执行 n 次。

示例:数字方法

puts 6.even?      # true
puts 7.odd?       # true
puts 8.3.ceil     # 9
puts 3.14.floor   # 3
puts 5.next       # 6
puts 5.pred       # 4
puts 12.gcd(8)    # 4
3.times { |num| puts "Number #{num}" }

输出

true
true
9
3
6
4
4
Number 0
Number 1
Number 2

Ruby 有理数

有理数是像 2/34/5 这样的分数。与浮点数不同,它们表示精确值,没有近似。

语法

Rational(numerator, denominator)

示例

num_rational = Rational(2, 3)
puts num_rational
# Output: 2/3

# Convert integer to rational number
puts 3.to_r
# Output: 3/1

# Convert float to rational
puts 0.5.to_r
# Output: 1/2

# Convert numerical string to rational
puts "0.5".to_r
# Output: 1/2

Ruby 复数

复数具有实部和虚部。例如,在复数 2 + 3i

  • 2实部
  • 3虚部,因为它由 i(虚数符号)表示。

语法

Complex(real, imag)

示例

complex1 = Complex(2, 3)
complex2 = Complex(3, 4)

puts complex1   # Output: 2+3i

# Get real and imaginary parts
puts complex1.real    # Output: 2
puts complex1.imag    # Output: 3

# Add complex numbers
puts complex1 + complex2    # Output: 5+7i

注意:在早于 2.0.0 的 Ruby 版本中,您可能需要添加 require 'complex' 来从标准库加载 Complex 库。从 Ruby 2.0.0 开始,此库已默认加载,因此无需额外代码。


Ruby BigDecimal

您可以使用 BigDecimal 来处理非常大或非常精确的十进制数。它对于精度很重要的金融或科学计算非常有用。

示例

require 'bigdecimal'

num_bd = BigDecimal("0.123456789123456789")
puts num_bd

# Output: 0.123456789123456789e0

程序工作方式如下:

代码 描述
require 'bigdecimal' BigDecimal 库加载到您的程序中。不加载它就无法创建 BigDecimal 数字。
BigDecimal("0.123456789123456789") 通过接受数值字符串作为其参数来创建 BigDecimal 数字。

常见问题

如何查找数字的数据类型?

您可以使用数字的 .class 方法来查找其类型。例如,

require 'bigdecimal'

puts 5.class
puts 5.5.class
puts Rational(2, 3).class
puts Complex(2, 3).class
puts BigDecimal("0.234598").class

输出

Integer
Float
Rational
Complex
BigDecimal
如何转换数字类型?

您可以使用以下方法在数字类型之间进行转换

  • to_i - 转换为整数。
  • to_f - 转换为浮点数。
  • to_r - 转换为有理数。

例如,

# Convert integer to float
puts 5.to_f   # Output: 5.0

# Convert float to integer
puts 5.5.to_i   # Output: 5

# Convert float to real number
puts 5.5.to_r   # Output: 11/2

# Convert rational number to float
puts Rational(11, 2).to_f   # Output: 5.5

# Convert rational number to integer
puts Rational(11, 2).to_i   # Output: 5

# Convert numerical string to integer
puts "5.5".to_i   # Output: 5

# Convert numerical string to float
puts "5.5".to_f   # Output: 5.5

# Convert numerical string to rational number
puts "5.5".to_r   # Output: 11/2
如何在 Ruby 中使用 Math 常量和方法?

您可以使用 Ruby 的 Math 模块来访问数学常量和函数

puts Math::PI       # Output: 3.141592653589793
puts Math.sqrt(16)  # Output: 4.0
puts Math.sin(0)    # Output: 0.0
为什么我们需要加载 BigDecimal 和 Complex 库?

在 Ruby 中,BigDecimalComplex 是 Ruby 标准库的一部分,而不是其核心库。因此,您需要使用以下代码手动加载它

# Load BigDecimal library
require 'bigdecimal'

# Load Complex library
require 'complex'

但是,许多 Ruby 版本默认加载 Complex 库(但不加载 BigDecimal 库)。

你觉得这篇文章有帮助吗?

我们的高级学习平台,凭借十多年的经验和数千条反馈创建。

以前所未有的方式学习和提高您的编程技能。

试用 Programiz PRO
  • 交互式课程
  • 证书
  • AI 帮助
  • 2000+ 挑战