Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference Between HUGE_VALF and INFINITY Constants

In OpenCL, there are two floating point math constants that represent infinity. One of them is simply INFINITY. The other, HUGE_VALF, "evaluates to" infinity.

What is the difference between these two? What does it mean to "evaluate to" infinity?

like image 453
benshope Avatar asked Dec 13 '13 01:12

benshope


1 Answers

HUGE_VALF is a legacy name that allows for floating-point systems that did not support infinities. For example, the C standard specifies that HUGE_VALF be returned in certain overflow cases. When a C implementation did not support infinities, HUGE_VALF would be the largest representable value. When an implementation did support infinities, HUGE_VALF would be infinity. The C standard still allows this, and I suppose there are some implementations around that still do not support infinities.

OpenCL uses IEEE 754 (more or less), so it does have infinities, so HUGE_VALF is infinity. By defining HUGE_VALF, OpenCL helps to support old C code being ported to OpenCL.

Regarding the wording that says “HUGE_VALF evaluates to +infinity” but INFINITY is “A constant expression of type float representing positive or unsigned infinity”, I would wonder whether that is intended to allow for some run-time preparation of HUGE_VALF (e.g., expressing it as 1./0.). However, the documentation also says that HUGE_VALF is “A positive float constant expression.” On the balance, that makes me think the wording is simply a bit sloppy, and that HUGE_VALF and INFINITY are equivalent.

like image 153
Eric Postpischil Avatar answered Sep 22 '22 11:09

Eric Postpischil