-9

Given the following snippet

int k = 12; float a = 1.0/12; if ( 1.0 / k == a ) printf("%d",0); 

I would expect the condition to be true, but it prints nothing.

What's the reason? well, maybe I am not polite,I am feel very sorry,this is my first time to ask a question on this sites,I don't know there will be someone answer me,so thank you for you to correct me,I will do better next time ,if you have spare time ,would you please solve my problem enter image description here

the following is my code,when it comes to the second example,it just print five equation enter image description here

6
  • 4
    What’s it expected to do? What does it actually do? It’s incomplete and float is spelled wrong. Show a minimal reproducible example. Commented Jan 8, 2020 at 8:47
  • The question is quite clear - OP doesn't understand floating point math and therefore doesn't understand why the code doesn't print anything. Commented Jan 8, 2020 at 8:52
  • @MarkTolonen it's a badly-transcribed 'explain the result' homework question. Commented Jan 8, 2020 at 8:53
  • <deleted> - I don't do homework for other people. Commented Jan 8, 2020 at 8:54
  • 1
    Concerning the programme you just added: avoid comparison between float numbers. Replace if (x == y) with a comparison between the absolute value of the difference and a very small value Commented Jan 8, 2020 at 10:00

1 Answer 1

2

You don't see anything because the condition results to false. Your variable a is float, while 1.0/k in if clause is double. They have different precision and therefore are not equal. In general it is a bad idea to compare floats on equality.

Sign up to request clarification or add additional context in comments.

6 Comments

well,maybe this problem is not well, I really want to know about an algorithm problem that I do recently and my answer is wrong.would you please give me you eamil so that I can consult you,thank you very much.
When I correctly understand your task, you need to find all such integer pairs (x, y) with x >= y such that for given k it holds: 1/k = 1/x + 1/y. In this case, you can consider 1/k = 1/x + 1/y as 1/k = (x + y) / xy or even as xy = (x+y)k so, you can run over x and y (in your case i and j) and check the condition above in integers
I know this solution,I just want to know why my answer is wrong,I find the range of x and y,I think my idea is right and the range is right as well,why I can't get the right answer
Your answer is wrong because not every real number can be represented by double. 1/k is some value stored in double format which is close to 1/k. It might be not exact as the double has limited amount of bits to store the value. Same for 1/x and 1/y. As the values are not exact the sum might be unequal to 1/k. Therefore I suggest to stick to integers whenever it is possible. If you need to deal with doubles you should take into account some bias.
in this problem, my code can output the 1/12 = 1/156 + 1/13,but it can't output 1/12 = 1/84 +1/14, if it can't represent by double,it just close to number,why the equaltion of 1/156 is right
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.