在C++中表示浮点无穷大的方法:

huangapple go评论96阅读模式
英文:

Ways to represent float infinity in C++

问题

我想在我的C++程序中表示浮点无穷大。
我找到了两种实现方法:使用INFINITYstd::numeric_limits<float>::infinity()

这两种选项似乎都可以工作,但我不确定哪种是更好的选择。
一方面,INFINITY是在math.h中定义的简单宏,易于使用。
另一方面,std::numeric_limits<float>::infinity()<limits>中的一个函数,属于C++ STL的一部分,似乎是一种传统的方式。

总结:

我应该使用INFINITY还是std::numeric_limits<float>::infinity()来表示C++程序中的浮点无穷大?
哪种方法被认为是更好的实践,是否有性能或可移植性方面的考虑我应该注意?

英文:

I want to express floating-point infinity in C++ for my program.
I came across two ways to achieve this: using INFINITY and std::numeric_limits&lt;float&gt;::infinity().

Both options seem to work, but I'm unsure which one is the better choice.
On one hand, INFINITY is a simple macro defined in math.h, making it easy to use.
On the other hand, std::numeric_limits&lt;float&gt;::infinity() is a function from &lt;limits&gt; and is part of the C++ STL, which seems to be a conventional way.

In summary:

Should I use INFINITY or std::numeric_limits&lt;float&gt;::infinity() to represent floating-point infinity in my C++ program?
Which one is considered a better practice, and are there any performance or portability considerations I should be aware of?

答案1

得分: 6

宏是在C中引入的,用于表达在语言中无法很好表达的内容。在这个特定的例子中,INFINITY 命名了一个编译时常量。

C++引入了不同的方式,在语言中允许你表达以前在C中需要宏的情况下用于表达的相同内容(在许多情况下,但不是全部情况下)。关于这个特定情况,C++有一种以各种方式命名编译时常量的方法。

INFINITY 相比,使用 std::numeric_limits&lt;float&gt;::infinity() 具有以下优点:

  • 它在一个命名空间内具有适当的作用域,这意味着其他代码可以使用标识符 infinity 来表示其他内容,而不会引发名称冲突。
  • 它是强类型的,命名了一个类型,因此 std::numeric_limits&lt;T&gt;::infinity() 可以针对不同的 T 值进行特化,并且可以在通用上下文中无需转换而使用。
  • 它是语言的一部分(而不是一个单独的预处理器语言),这使得第三方工具(例如IDE)更容易与之交互。

出于历史原因,infinity() 是一个 函数,但这并不是特别重要,因为(因为它是 constexpr)你可以在任何可以使用其他常量表达式的上下文中使用它。

相反,与 std::numeric_limits&lt;float&gt;::infinity() 相比,使用 INFINITY 具有以下优点:

  • 它更短。
英文:

Macros were introduced in C to express things that couldn’t be expressed well otherwise in the language. In this particular example, INFINITY names a compile-time constant.

C++ introduces different ways into the language that allow you to express the same things that you used to need macros for in C (in many, but not all circumstances). Regarding this particular case, C++ has a way of naming compile-time constants in various ways.

Compared to INFINITY, using std::numeric_limits&lt;float&gt;::infinity() has the following advantages:

  • It is properly scoped inside a namespace, which means that other code can use the identifier infinity to denote other things without causing name clashes.
  • It is strongly typed and names a type, so that std::numeric_limits&lt;T&gt;::infinity() can be specialised for different values of T, and it can be used without conversion in generic contexts.
  • It is part of the language (rather than a separate preprocessor language), which makes it easier for third-party tooling (e.g. IDEs) to interact with it.

For historical reasons, infinity() is a function but this isn’t terribly important because (since it’s constexpr) you can use it in any context in which you could use any other constant expression.

Conversely, compared to std::numeric_limits&lt;float&gt;::infinity(), using INFINITY has the following advantages:

  • It is shorter.

huangapple
  • 本文由 发表于 2023年7月23日 16:35:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76747311.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定