1

The Following is my code

#include<stdio.h>
int main( void )

{
    double dInput = 0.7;        
    printf("\n %.17f ",dInput);
    return 0;
}

The output I get is 0.69999999999999996 instead of 0.7

can anyone explain why this is happening.

haccks
  • 104,019
  • 25
  • 176
  • 264
user
  • 33
  • 8
  • 1
    Its probably the same problem as in this question: http://stackoverflow.com/questions/1089018/why-cant-decimal-numbers-be-represented-exactly-in-binary – fjc Nov 21 '13 at 12:50
  • 10
    [What Every Computer Scientist Should Know About Floating Point Arithmetic](http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.22.6768) – devnull Nov 21 '13 at 12:50
  • 2
    @KlasLindbäck; No. That's different issue. – haccks Nov 21 '13 at 12:53
  • It is extremely strange that you get `0.6999999`. You should get `0.69999999999999996`, with perhaps, if you are using an inferior C implementation, only some variation in the last decimal digit. – Pascal Cuoq Nov 21 '13 at 12:54
  • 2
    I wish people actually looked at the linked question before closing as duplicate... That question is about usage of `float` vs. `double`. – interjay Nov 21 '13 at 12:54
  • @interjay; Agreed with you. Voted to reopen. – haccks Nov 21 '13 at 12:55
  • @interjay What “that” question are you talking about? http://stackoverflow.com/questions/1089018/why-cant-decimal-numbers-be-represented-exactly-in-binary is about “Why can't decimal numbers be represented exactly in binary?”, which is about how this question should be phrased. – Pascal Cuoq Nov 21 '13 at 12:56
  • 1
    @PascalCuoq; The link you are talking about is right but the the duplicate marked link is not explaining this issue anyway. – haccks Nov 21 '13 at 12:59
  • 1
    @interjay This ought to be marked as a dup of [this](http://stackoverflow.com/questions/6230869/tiny-numbers-in-place-of-zero). – devnull Nov 21 '13 at 13:00
  • @haccks I fail to understand the rationale behind voting to reopen this. Agreed that the question being referred to doesn't answer _this_ the best, there are scores of questions that answer __this__ very problem. I'm certain that you'd have answered it several times. – devnull Nov 21 '13 at 13:06
  • 2
    @devnull; Yes right. But I am disagree with wrong close votes. If this question will reopen then I will vote again to close it with its right dupe. By the way I have flaged it for moderator attention. – haccks Nov 21 '13 at 13:09
  • Similar question: http://stackoverflow.com/questions/19871086/float-variable-doesnt-meet-the-conditions-c/19871124 – Michael Nov 21 '13 at 13:35
  • this is the 1000001th question about floating point in this site – phuclv Nov 21 '13 at 13:43
  • 1
    I foud a great explanation on wiki in the section: **[Accuracy problems](http://en.wikipedia.org/wiki/Floating_point)** – haccks Nov 21 '13 at 14:09
  • @haccks Your link does not include deleted questions. – Pascal Cuoq Nov 21 '13 at 21:02
  • @PascalCuoq; What? I do not understand. Which deleted questions? – haccks Nov 21 '13 at 21:23
  • @haccks Bad StackOverflow questions get deleted. You cannot see these questions through your link http://stackoverflow.com/questions/tagged/floating-point . There is no reason to think that a significant proportion of floating-point questions that were asked were not deleted. It certainly feels like 1000000. (Also, some identical questions are asked without the floating-point tag) – Pascal Cuoq Nov 21 '13 at 21:39
  • @PascalCuoq; I was not aware about the deleted ones (deleting my comment). When could I see that? – haccks Nov 21 '13 at 21:43

0 Answers0