maxf
2026/4/14大约 3 分钟
最后同步日期:2026-04-15 | Godot 官方原文 — maxf
maxf
定义
maxf() 用来比较两个浮点数(小数),返回更大的那个。
就像你看两把尺子的刻度,只取更长的那个——maxf(3.5, 7.2) 会返回 7.2。
在游戏开发中,经常用它来设置"最低门槛"。比如确保角色速度不会低于某个值、保证计时器不会变成负数等。和 max() 的区别在于,maxf() 明确只处理浮点数(带小数点的数),不会和整数混淆。
函数签名
C#
public static float Max(float a, float b)GDScript
func maxf(a: float, b: float) -> float参数说明
| 参数 | 类型 | 必需 | 说明 |
|---|---|---|---|
a | float | 是 | 第一个浮点数 |
b | float | 是 | 第二个浮点数 |
返回值
float —— a 和 b 中较大的那个值。如果两个相等,返回任意一个(即 a)。
代码示例
C#
// ===== 基础用法:比较两个浮点数 =====
float bigger = Mathf.Max(3.5f, 7.2f);
// 运行结果: bigger = 7.2
float smaller = Mathf.Max(10.0f, 2.0f);
// 运行结果: smaller = 10.0
// 两数相等,返回 a
float equal = Mathf.Max(5.0f, 5.0f);
// 运行结果: equal = 5.0
// ===== 实际场景:音频系统 —— 音量不低于最低值 =====
[Export] public float ExMinVolume = 0.1f; // 最低音量,防止完全静音
private float _currentVolume = 0.5f;
public void SetVolume(float newVolume)
{
// 音量不能低于最低值,但可以往上加
_currentVolume = Mathf.Max(ExMinVolume, newVolume);
GD.Print($"当前音量: {_currentVolume}");
}
// 调用示例:
// SetVolume(0.8f) → 运行结果: 当前音量: 0.8
// SetVolume(-0.3f) → 运行结果: 当前音量: 0.1(被最低值兜底)
// SetVolume(0.0f) → 运行结果: 当前音量: 0.1(被最低值兜底)
// ===== 进阶用法:冷却时间不会低于 0 =====
[Export] public float ExCooldownDuration = 2.0f;
private float _cooldownRemaining = 0.0f;
public override void _Process(double delta)
{
// 冷却时间逐帧减少
_cooldownRemaining -= (float)delta;
// 冷却时间不能低于 0
_cooldownRemaining = Mathf.Max(0.0f, _cooldownRemaining);
if (_cooldownRemaining <= 0.0f)
{
GD.Print("技能已就绪!");
}
}
public void UseSkill()
{
_cooldownRemaining = ExCooldownDuration;
GD.Print($"技能使用,冷却 {_cooldownRemaining} 秒");
}
// 运行结果(持续几帧后):
// 技能使用,冷却 2 秒
// 技能已就绪!(冷却结束后持续输出)GDScript
# ===== 基础用法:比较两个浮点数 =====
var bigger = maxf(3.5, 7.2)
# 运行结果: bigger = 7.2
var smaller = maxf(10.0, 2.0)
# 运行结果: smaller = 10.0
# 两数相等,返回 a
var equal = maxf(5.0, 5.0)
# 运行结果: equal = 5.0
# ===== 实际场景:音频系统 —— 音量不低于最低值 =====
@export var ex_min_volume: float = 0.1 # 最低音量,防止完全静音
var _current_volume: float = 0.5
func set_volume(new_volume: float) -> void:
# 音量不能低于最低值,但可以往上加
_current_volume = maxf(ex_min_volume, new_volume)
print("当前音量: %f" % _current_volume)
# 调用示例:
# set_volume(0.8) → 运行结果: 当前音量: 0.8
# set_volume(-0.3) → 运行结果: 当前音量: 0.1(被最低值兜底)
# set_volume(0.0) → 运行结果: 当前音量: 0.1(被最低值兜底)
# ===== 进阶用法:冷却时间不会低于 0 =====
@export var ex_cooldown_duration: float = 2.0
var _cooldown_remaining: float = 0.0
func _process(delta: float) -> void:
# 冷却时间逐帧减少
_cooldown_remaining -= delta
# 冷却时间不能低于 0
_cooldown_remaining = maxf(0.0, _cooldown_remaining)
if _cooldown_remaining <= 0.0:
print("技能已就绪!")
func use_skill() -> void:
_cooldown_remaining = ex_cooldown_duration
print("技能使用,冷却 %f 秒" % _cooldown_remaining)
# 运行结果(持续几帧后):
# 技能使用,冷却 2.0 秒
# 技能已就绪!(冷却结束后持续输出)注意事项
- 此函数只接受浮点数(float)参数。如果需要比较整数,请使用
maxi()或max()。 - C# 中可使用
Mathf.Max()或System.Math.Max(),两者功能相同。Mathf.Max返回float,System.Math.Max返回double。 - 如果需要同时限制最大值和最小值,请使用
clampf()。 - 浮点数比较可能受精度影响。如果需要判断两个浮点数是否"近似相等",请使用
is_equal_approx()。 maxf()的核心用途是设置下限(floor),而不是找最大值。大多数时候你写的是maxf(底线, 实际值),确保结果不会低于底线。
