Skip to main content
gamedev.net
🔒 Locked

reciprocal

Started by BloodLust666 2 replies 450+ views
Original Post
BloodLust666
BloodLust666
is there a reciprocal function for C++? yea i could just make one but if there's a standard one then i'll use it. i mainly found the problem when i tried (1/x) where x became 0 rarely, but still possible..
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
K_I_L_E_R
K_I_L_E_R
create your own function that handles the "div by zero" issue.
Otherwise just do:

x = (x == 0) ? x : (1/x);

EDIT: I would just like to say that I'm implying there is no such function in the std library.

[Edited by - K_I_L_E_R on October 19, 2005 12:14:46 AM]
ury
ury
In order to avoid underflows and denormals, I'd really recommend using something like:

y = (abs(x) < EPSLILON) ? 0.0 : (1.0/x);

EPSILON depends on the floating point precission.
JohnBolton
JohnBolton
Quote:
Original post by ury
In order to avoid underflows and denormals, I'd really recommend using something like:

y = (abs(x) < EPSLILON) ? 0.0 : (1.0/x);

EPSILON depends on the floating point precission.


fabs (or fabsf), not abs
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.