2

I have a issue with basic typecasting.

#include<stdio.h>

int main()
{
     printf("%.22f",0.30);
     return 1;
}

The output I am getting is 0.2999999999999999888978

Why is 0.3 converted to a lesser value

Any help much appreciated

Nandish

Mankarse
  • 39,818
  • 11
  • 97
  • 141
Nandish A
  • 1,645
  • 5
  • 26
  • 41
  • 3
    No, you have an issue with your inability to search before asking a question that's been asked a gazillion times before. See http://stackoverflow.com/faq#searching – paxdiablo Oct 12 '11 at 07:06
  • 2
    Thank you Mr Paxdiablo. But you don't need to be so harsh putting across your thoughts about my search abilities – Nandish A Oct 12 '11 at 07:16
  • I know [this question says .NET in the can](http://stackoverflow.com/questions/1420752/is-double-multiplication-broken-in-net), but it's really the same thing. – R. Martinho Fernandes Oct 12 '11 at 07:24
  • 1
    I wasn't trying to be harsh, I was helping out by pointing you to the relevant section of the FAQ, detailing _exactly_ how searching works. Being harsh would have been pointing out that, as someone who has been a member here for seven months, you should have figured it out by now :-) Try to take it as helpful advice rather than criticism. – paxdiablo Oct 12 '11 at 07:33

1 Answers1

9

This is because 0.30 cannot be exactly represented in binary floating-point. Internally, only an approximation to 0.30 can be stored. Therefore, when you print it all out, you'll get a slightly different number.

http://en.wikipedia.org/wiki/Floating_point

Mysticial
  • 464,885
  • 45
  • 335
  • 332
  • @NandishA: please consider accepting the most appropriate answer of your question, if the issue is resolved or question answered, to keep up the enthusiasm of users. – phoxis Oct 12 '11 at 12:30