# Trying to understand this bit of C code

This topic is 2566 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, I'm using C# and I came across this function (operator) which I'm not sure how to interpret.

#define C_FPREM (_finite(f[0]/f[1]) ? f[0]-(TRUNC(f[0]/f[1])?f[1]): f[0]/f[1])

My only confusion is this:
-Does that mean that if f[0]/f[1] is infinity, that infinity will be returned?

It should be obvious that that's the case but I just can't grasp why somebody would write a #define like this, and I'm not sure about my C skills anyway.

My other question is:
-If we get 0/0, will C return true or false for the _finite() function, or just a NaN ?
Maybe someone using C can try it out and simply tell me.

Thanks

##### Share on other sites

-Does that mean that if f[0]/f[1] is infinity, that infinity will be returned?

Yes.

It should be obvious that that's the case but I just can't grasp why somebody would write a #define like this, and I'm not sure about my C skills anyway.[/quote]
This macro computes a partial remainder. What is the remainder in any division involving infinity? Completely meaningless, so we may as well just propagate infinity through to the result.

-If we get 0/0, will C return true or false for the _finite() function, or just a NaN ?[/quote]
Does 0/0 = NaN on x86? I know that PowerPC processors evaluate 0/0 = 0, but I can't recall for x86.

In any case, _finite(NaN) will evaluate to NaN. It only checks for infinity, not invalid numbers.

##### Share on other sites
Umm, _finite() returns an int so it can't return NaN. It returns zero for both infinities and NaNs.

I'm also confident that 0.0/0.0 is a NaN according to the IEEE spec, so it should end up as NaN on any modern CPU, including PowerPC ones.

##### Share on other sites

Umm, _finite() returns an int so it can't return NaN. It returns zero for both infinities and NaNs.

Good catch.

I'm also confident that 0.0/0.0 is a NaN according to the IEEE spec, so it should end up as NaN on any modern CPU, including PowerPC ones.[/quote]
Where does it say that in the IEEE spec? I couldn't find it upon a brief inspection.

You may indeed be right, but for historical reasons PowerPC's don't have any support for integer divide-by-zero either (unlike on x86 where it triggers an exception), and IIRC similar reasoning applied to floats.

##### Share on other sites

This is just C code trying to mimic assembly operators.

##### Share on other sites

I'm also confident that 0.0/0.0 is a NaN according to the IEEE spec, so it should end up as NaN on any modern CPU, including PowerPC ones.

Where does it say that in the IEEE spec? I couldn't find it upon a brief inspection.
[/quote]
I don't have a copy of the spec, but this document (see around the top of page 7) indicates that it does require 0.0/0.0 to be a NaN.

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5

• 9
• 9
• 11
• 11
• 23
• ### Forum Statistics

• Total Topics
633677
• Total Posts
3013284
×

## Important Information

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!