ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I want to see if a*b=1, that is a=1/b
a and b are float
if (a*b==1)
never seems to work (for, let's say a=1.0/2.0 and b=2.0)
Why is that?
What exactly do you mean with "never seems to work". What happens? Does it compile and link without errors and warnings? Does it ouput any errors? Or does it give you wrong answers? What is the code you tried?
With floating point numbers, your result may be 1.9999999999999 or something similar. A floating point fraction (base 10) may not be exactly convertable to a base 2 floating point number.
Instead of an equality test for floating point numbers you should check if the difference is below a small delta value.
For example, if you use a floating point variable to control a for loop and you test for equality to terminate the root, then the loop may not end.
The use of these macros is advisable, are iso standard and maybe considered best practice but windows maths header does not include them; I normally code on windows (because theres just is not an ide which compares to visual studio in my opinion) and therefore use the above method rather than the macros. As I want my apps to work on both windows and nix.
The storage data format for float in memory consists of integer data and its precision information. Thats why it doesnt works as it seems to be in " if(condition)" , because this loops works well only for integer values or binary values. In case of float this loop compares the precision values and gives unexpected results, sometimes it works.
better way for float comparision is to first separate the integer and data parts from the float byte.
The use of these macros is advisable, are iso standard and maybe considered best practice but windows maths header does not include them; I normally code on windows (because theres just is not an ide which compares to visual studio in my opinion) and therefore use the above method rather than the macros. As I want my apps to work on both windows and nix.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.