Floating-Point imprecision fix

Description: General discussion regarding the current engine development.
Moderator: Moderators

CaptainSwag101 M
Topic author, Moderator
Moderator
Avatar
CaptainSwag101 M
Topic author, Moderator
Moderator
Age: 20
Reputation: 20
Posts: 113
Joined: 2 Apr 2014
Location: California, United States
YouTube

Post #1by CaptainSwag101 » 10 Apr 2014, 22:48

(Ok, so I didn't know if I could really put this in another section, so I created a new topic, I hope everyone is OK with me doing that. If not, I am sorry and I will move this post somewhere else)

Ok so I don't know if this will help or impress anybody, but a few days ago I thought of a way to check if a floating point value is imprecise and fix it. As you probably know, if you attempt to add or subtract from a float, but the value which you add or subtract is too small, the float value will not change. This could possibly lead to errors. However, I have created a very basic function that should determine the correct amount to subtract from the float in order for its value to change. Just paste this into your code, and it should work:

Code: Select all

float fixFloat(float myFloat, int value, int exponent)
{
   /*
   If myFloat minus "value" is still the original float,
   change the scientific notation (<-- defined as "exponent") of "value"
   until myFloat - (value * (10 ^ exponent)) != myFloat
   */
   
   while(myFloat - (value * (10 ^ exponent)) == myFloat)
   {
      exponent += 1;
   }

   myFloat -= (value * (10 ^ exponent));
   
   return myFloat;
}
Image
--The Captain of Swag

Return to “Engine Development”

Who is online (over the past 5 minutes)

Users browsing this forum: 1 guest