-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

  • 4
    What’s it expected to do? What does it actually do? It’s incomplete and float is spelled wrong. Show a [mcve]. – Mark Tolonen Jan 08 '20 at 08: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. – Myst Jan 08 '20 at 08:52
  • @MarkTolonen it's a badly-transcribed 'explain the result' homework question. – Martin James Jan 08 '20 at 08:53
  • - I don't do homework for other people. – Myst Jan 08 '20 at 08:54
  • @MarkTolonen - Oh... I didn't think about that ... thanks. I definitely didn't plan (nor desire) to do someone's homework :-/ – Myst Jan 08 '20 at 08:56
  • 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 – Damien Jan 08 '20 at 10:00

1 Answers1

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.

rhaport
  • 540
  • 3
  • 11
  • 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. – Shelly Cooper Jan 08 '20 at 09:46
  • 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 – rhaport Jan 08 '20 at 10:21
  • 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 – Shelly Cooper Jan 09 '20 at 04:46
  • 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. – rhaport Jan 09 '20 at 07:14
  • 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 – Shelly Cooper Jan 10 '20 at 00:34
  • @ShellyCooper as I said it might be not exact. But sometimes it works. Therefore with some values you get expected results and with some not. You can refer to this paper for more information https://www.itu.dk/~sestoft/bachelor/IEEE754_article.pdf – rhaport Jan 10 '20 at 03:59