Help with simple programming

This is a discussion on Help with simple programming within the C++ Programming forums, part of the General Programming Boards category; Question about the modf function from math.h. It uses double instead of float. It can't be used with a float ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    50

    Help with simple programming

    Question about the modf function from math.h. It uses double instead of float. It can't be used with a float variable . Correct ? Is there any function similar to this that can ? The problem I am having is when I input 9.1 it spits out .09999999 . When I put in 9.2 , it is fine and spits out .2 . What's the deal and what can I do about it?

    Thanks for any replies.

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    50
    I got it. Nevermind. Yeah, feels good to fix something even if I just took someone elses header file to do it.

    I found a round.h header file.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    Welcome to the world of floating point numbers

    http://docs.sun.com/source/806-3568/ncg_goldberg.html
    Not every number has an exact floating point representation - in fact the vast majority do not.
    So you need to anticipate all these minor inaccuracies and make sure your code works in such a way as to minimise the effects.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    50
    my code works. I've gotten quite far tonight. Farther than I ever have. I guess you need a passion for it to really code. I got passion for what I'm currently doing. I'm just coding for recreational use right now. I'm not a professional. I haven't coded for months and when I did, I was trying to do graphics which was too hard for me.

    Anyway, the statement:

    for (n=2; n<15; n++)

    Does n start on 2 or 3 when it begins ?

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    for (n=2; n<15; n++) cout << n;
    Find out - it's really easy
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating very simple text editor using c
    By if13121 in forum C Programming
    Replies: 9
    Last Post: 10-19-2010, 05:26 PM
  2. Simple message encryption
    By Vicious in forum C++ Programming
    Replies: 10
    Last Post: 11-07-2004, 10:48 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Simple simple program
    By Ryback in forum C++ Programming
    Replies: 10
    Last Post: 09-09-2004, 05:48 AM
  5. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 07:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21