Jump to content
  • Advertisement
Sign in to follow this  
braincell

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.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

In the header it's

#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 this post


Link to post
Share on other sites
Advertisement

-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 this post


Link to post
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 this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

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!