Thread: Easy Float Compared to Int Question

  1. #1
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230

    Easy Float Compared to Int Question

    Hi guys, so I made this little thing for a bit of fun lol, just to see if I knew how to use simple classes properly (because I'm learning about them), all works perfectly except for the fact that when I enter a integer such as 15, it doesn't return a remainder for the result. If I enter 15 then it should return 14.5 but it returns 14. Which leads me to believe that something is wrong with the float? I have no idea, I can't see anything wrong Here is the code:

    Code:
    #include <iostream>
    
    using namespace std;
    
    class agetest {
    public:
    	unsigned float calc_age(unsigned float);
    };
    
    void main() {
    	unsigned int age;
    	cout << "Is your girlfriend too young for you? Find out the easy way!" << endl << "Enter your age: ";
    	cin >> age;
    	cin.ignore();
    	agetest person;
    	cout << "You shouldn't be dating anyone younger than: " << person.calc_age(age);
    	cin.get();
    }
    
    unsigned float agetest::calc_age(unsigned float age) {
    	age = age / 2 + 7;
    	return age;
    }
    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Unsigned float is invalid in gcc and visual studio, so I'm quite surprised that you managed to compile it at all - what compiler are you using?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230
    visual studio c++ 2008 express, and it compiled with no errors or warnings. hmmm so what shall I do, just use float instead of unsigned? Is there no such thing as an unsigned float or something? Why is it invalid?
    Last edited by pobri19; 08-25-2008 at 04:28 AM.
    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, I'm VERY surprised it compiles without errors, as both g++ and Visual Studio .Net complain about that very thing. Are you sure you are actually compiling the code you posted?

    Sorry, but I can't point to the exact statement in the C standard (which is followed identically by C++) where it states that signed/unsigned in conjuncion with float is invalid, but unsigned floating point numbers do not exist in the real world hardware - it would only give 0.3 extra decimal points to remove the sign-bit anyways.

    Unsigned and signed integers integers do exist, and there is a real gain as the size of the integer is essentially doubled by using unsigned (and it's only comparison/condition situations where the sign matter for integers, so the additional number of instructions is much smaller (about 5-6 extra condition codes) than doubling the number of floating point formats - which would essentially double the number of floating point operations, where there already is a good three-four dozen instructions).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230
    Alright Matt well thanks for clearing that up for me. It works perfectly by specifying non-unsigned Cheers for the help bud. Oh and yes, I'm sure I was compiling that code .
    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It does compile with VC++ (with warnings) and appears to discard the float, treating "unsigned float" as simply "unsigned (int)". IMO, stupid behaviour since the code obviously won't do what is intended.

    In addition, the return type of main is int, not void.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by anon View Post
    It does compile with VC++ (with warnings) and appears to discard the float, treating "unsigned float" as simply "unsigned (int)". IMO, stupid behaviour since the code obviously won't do what is intended.
    Surely that's a bug^H^H^Hfeature of the Microsoft compiler, rather than an intentional functionality according to the C++ specification?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by matsp View Post
    Surely that's a bug^H^H^Hfeature of the Microsoft compiler, rather than an intentional functionality according to the C++ specification?
    Most definitely. See 7.1.5 in the standard.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230
    Hey guys another thing, the above program when compiled (after float was fixed) does not run on any computer other than the one it was compiled on... Even if the operating systems are the same. I've tried to run it from a computer at my school, just to see if I could, and I've sent it to a friend to see if it would work on his, and neither work, they get this error message:

    W:\>"D:\Documents and Settings\ktaton\My Documents\project.exe"
    The system cannot execute the specified program.

    And my friend gets the same error. It's the first time I've made something that I've actually sent to someone to try on their computer. Is there something I'm doing wrong? Do they need extra files other than just the compiled .exe?

    Thanks.
    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You'll need to also give them the Visual Studio Redistribution Library (or some such term), which has the runtime libraries.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NEED HELP READING FILE and PRINTING
    By geoffr0 in forum C Programming
    Replies: 4
    Last Post: 04-16-2009, 05:26 PM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 05:34 PM
  4. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM