Allegro.cc Forums » Programming Questions » AGAIN floats

 This thread is locked; no one can reply to it. 1   2
 AGAIN floats
 Frank Drebin Member #2,987 December 2002 i want to write a function ```bool floats_are_equal(float a, float b) { return (fabs(a-b)
 Evert Member #794 November 2000 Quote:because floats have 6 decimals Not exactly. Floats can store numbers with a certain precision (don't know if that's six or more, but that doesn't really matter), that precission does not depend on the order of magnitude of the number (because it is a floating-point number).Depending on your needs, 0.000001 could be a sufficient check. It really depends on what you want to do with it.
 spellcaster Member #1,493 September 2001 First of all, you need to turn around the operator, or it'll return true if the floats are not equal.And I'd say use something which makes sense in your game. If you're using 4 decimal digits (internally) use 0,00005 as error. --There are no stupid questions, but there are a lot of inquisitive idiots.
 damage Member #3,438 April 2003 If you are using C++, do this. ```#include using namespace std; ... cout << "Float has " << numeric_limits.digits10 << " digits" << endl ``` Secondly, don't use float. Use double, you get more precision for free, I am pretty sure. ____Don't have anything private. Don't do anything silly like having a hidden name and address field with get_name and set_address and get_name and set_name functions. - Bjarne Stroustrop, creator of C++
 Evert Member #794 November 2000 doubles may be slower than floats (usually not an issue on modern hardware).
 gnolam Member #2,030 March 2002 damage said:Use double, you get more precision for free, I am pretty sure. It's a trade-off between accuracy and speed.Frank Drebin said:with float_error=0.000001 because floats have 6 decimals or is there a better value i can take for this (do have floats a maximum inaccuracy i should take?)? http://www.infosys.utas.edu.au/info/documentation/C/CStdLib.html#float.hFor further information, consult your local float.h ... -- Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!
 Chris Katko Member #1,881 January 2002 To my knowledge, doubles will normally be slower then floats until we go 64bit processors (doubles are 64bit). Unless in the case of using MMX, but then you have to reset the MMX registers and that takes up most of the gain, unless your doing batches of floating-point numbers.I may be wrong. I haven't been able to code in a long time so it may or may not be correct. -----sig:“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs"Political Correctness is fascism disguised as manners" --George Carlin
 damage Member #3,438 April 2003 Quote: doubles may be slower than floats (usually not an issue on modern hardware). That's true, but the vast majority of us will be programming on an x86 pentium or greater. From the pentium onward double and float have exactly the same speed, AFAICT. ____Don't have anything private. Don't do anything silly like having a hidden name and address field with get_name and set_address and get_name and set_name functions. - Bjarne Stroustrop, creator of C++
 A J Member #3,025 December 2002 does the libc have #DEFINEs for floating point error for operations on floats ? ___________________________The more you talk, the more AJ is right. - ML
 Frank Drebin Member #2,987 December 2002 the question isn't wehter using floats or doubles.i'm going to use floats. but what value should i take??? (p.s. is FLT_EPSILON in the float.h the maximum inaccurcay of floats?)?
 Mars Member #971 February 2001 Quote:the question isn't wehter using floats or doubles. Who are you blaming? They stayed relatively close to the topic! Quote:i'm going to use floats. but what value should i take You already have got some good replies. It depends on your application. If you're programming a game, then you need to take into consideration the accuracy of your engine. -- This posting is a natural product. The slight variations in spelling and grammar enhance its individual character and beauty and in no way are to be considered flaws or defects.
 X-G Member #856 December 2000 I'm still trying to figure out why exactly you need to do this comparison. So far, I've never had to do == checks on floats or doubles, simply because I've never come across any situation that would require it. -- Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散！悪霊退散！怨霊、物の怪、困った時は　ドーマン！セーマン！ドーマン！セーマン！　直ぐに呼びましょう陰陽師レッツゴー！
 Plucky Member #1,346 May 2001 Try: ```bool floats_are_equal(float a, float b) { return (fabs(a-b)/fabs(a) <= FLT_EPSILON) && (fabs(a-b)/fabs(b) <= FLT_EPSILON); // && can be replaced with || for a more relaxed comparison } ``` Should solve problems with scale issues and over/underflow issues.
 Frank Drebin Member #2,987 December 2002 i use this because my coordinates of objects are stored in floats and because of movement and collision detection i have to compare them!!!--> so there are max 3 places before the point and now when there are 3 places behind the point left -> i should take a value like 0.001 ?!?
 X-G Member #856 December 2000 Compare then, yes - but why == ? Collision detection usually just involves <= and >= ... -- Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散！悪霊退散！怨霊、物の怪、困った時は　ドーマン！セーマン！ドーマン！セーマン！　直ぐに呼びましょう陰陽師レッツゴー！
 Frank Drebin Member #2,987 December 2002 that's true but for movement ( while (x_pos!=x_pos_to_reach) move_x();) or something like this sometimes ==/!= is nice.
 X-G Member #856 December 2000 You could use use a threshold for that purpose and use . -- Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散！悪霊退散！怨霊、物の怪、困った時は　ドーマン！セーマン！ドーマン！セーマン！　直ぐに呼びましょう陰陽師レッツゴー！
 Fladimir da Gorf Member #1,565 October 2001 You can use, for example:```if( x_pos > x_pos_to_reach ) while( x_pos < x_pos_to_reach ) move_x(); else while( x_pos > x_pos_to_reach ) move_x(); ``` I've never, ever yet needed to compare floats, and I don't think I'll ever need to. OpenLayer has reached a random SVN version number ;) | Online manual | Installation video!| MSVC projects now possible with cmake | Now alvailable as a Dev-C++ Devpack! (Thanks to Kotori)
 Frank Drebin Member #2,987 December 2002 yes but i think mine is a bit shorter (easier).i don't like floats very much too but you have to use them for smooth movement (except scaled ints)
 Fladimir da Gorf Member #1,565 October 2001 "i don't like floats very much too "Who said I didn't like floats? When was the last time I didn't use floats (or doubles) for movement? I don't like comparasions between them, just because you may not reach the destination, depending on the speed of the object. "yes but i think mine is a bit shorter (easier)."Yes, but mine works. OpenLayer has reached a random SVN version number ;) | Online manual | Installation video!| MSVC projects now possible with cmake | Now alvailable as a Dev-C++ Devpack! (Thanks to Kotori)