A strange C++ problem????
Results 1 to 5 of 5

Thread: A strange C++ problem????

  1. #1
    Banned
    Join Date
    Aug 2004
    Posts
    19

    A strange C++ problem????

    I have got a strange c++ problem in which the code is not behaving properly as it should.

    The code is

    #include<iostream.h>
    #include<conio.h>
    void main()
    { float a=0.7;
    if(a<0.7)
    cout<<"c";
    else
    cout<<"c++";
    getch();
    }

    The problem is that for 0.7 it prints c and for any other value like 0.6 or 0.8 it prints c++ when both 0.7 numbers are replaced.
    I can,t figure out how this is so.
    Please help me as fast as you can

  2. #2
    AntiOnline n00b
    Join Date
    Feb 2004
    Posts
    665
    Hi,

    Puters puters what can i say

    Ok Lets see .............Do you have any prior knowledge of Floating Point Arthematics or how computers handle Floating Point Numbers?


    you are storing a double precision float value in single precision float location ..........The number 0.7 is being rounded off to around 6999999888 something like that .............thats the closed you get with double preceson ............. which is less than 0.7 thus C is printed.

    to see this insert a printf statement and see the value of a printf("a=%10.10g",a);


    to correct this what you can do is

    if(a<0.7f)

    now both sides are single precition and thus if would evaluate correctly. ..........this problem has it's root in the fact that Computer has limited Memory and the Binary thingy ............those fraction that can be easily written in decimal can streach to amzaing lengths in Binary Syatem and as we know Computer memory is still limited .

    If you want to read about Floating Point Arthematic read here


    --Good Luck--

  3. #3
    Ninja Code Monkey
    Join Date
    Nov 2001
    Location
    Washington State
    Posts
    1,027
    Hi. I'd recommend you learn how to use your debugger, and liberally use print statements for debugging your code.
    "When I get a little money I buy books; and if any is left I buy food and clothes." - Erasmus
    "There is no programming language, no matter how structured, that will prevent programmers from writing bad programs." - L. Flon
    "Mischief my ass, you are an unethical moron." - chsh
    Blog of X

  4. #4
    Banned
    Join Date
    Aug 2004
    Posts
    19
    Hi!
    Why is it not so with any other number except 0.7
    I've tried many numbers but they seem to print c++.
    Well I'm at college right now in hostal and will try to see the links when i reach home.
    Thanx

  5. #5
    AntiOnline n00b
    Join Date
    Feb 2004
    Posts
    665
    Hi,

    Originally posted here by AbhishekD
    Hi!
    Why is it not so with any other number except 0.7
    I've tried many numbers but they seem to print c++.
    Buddy listen to what Juridian said and you would have known it by itself why this is happening.

    Debugging

    Insert print statements.........insert watches on your variable to keep track of their values keep track .

    AbhishekD Insert a printf (printf("a=%10.10g",a); ) statement into your code and keep a check of the value of the variable a and you would notice

    a=0.5

    printf returns .5
    prints C++


    a=.9

    Printf returns .8999999762
    Prints C

    a=.11
    printf returns .10999994
    prints C

    So it's not just .7 ...............and again it's got to do with how Floating points are handeled in BINARY .

    I know it's a bit confusing............what you got to remember is we work on Desimal system and computrs work on BINARY............and until you iwll learn to create floating point represation in Binary it would be a bit difficult to understand ths unexpected result.............So try working on Floating Point Represation for a while to get a zist of it .

    Using Double insted of Float or adding a f (e.g 0.7f) to the RHS of the condidion will solve your problem ......but that you have run into this problem i qould suggest you get your hands wet with Floating Point Arthametic.

    Read the links i gave you about floating point arthematics or get a any Good Book on Computer Artecture it's generally there indepth.


    --Good Luck---

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

 Security News

     Patches

       Security Trends

         How-To

           Buying Guides