Thread: decimals

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    37

    decimals

    Can someone help me get started, or show me an example of how to allow a decimal (but only one) in an integer?

    Thank you,
    R

  2. #2
    float?
    Code:
    float f = 4.0
    int i = 4
    f != i

  3. #3
    Unregistered
    Guest

    i have the same question

    I was actually trying to round up floats wrt another float (too big to explain..)

    anyway, when I was using the for loop... it was like

    for (i=0; i<=10; i=i+.001)
    {
    and i did some complicated math **** over here..
    }

    but i never got the results..
    so i debugged using printf..
    and i found out that the i value
    is going beyond..
    1.001 (something like 1.001004, etc etc)

    so how do i restrict this to only 3 digits after the decimal ?
    is it done in the same way ?

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >how to allow a decimal (but only one) in an integer?
    An integer doesn't support floating point arithmetic, that's why there are floats and doubles.

    >I was actually trying to round up floats wrt another float
    Search the boards and you'll find a rounding function that I've posted.

    >for (i=0; i<=10; i=i+.001)
    Umm...

    >so how do i restrict this to only 3 digits after the decimal?
    If you know how to use floating point values correctly, then you shouldn't have to worry about extra precision that you don't need. I stress knowing what your doing because floating point arithmetic can be tricky.

    -Prelude
    My best code is written with the delete key.

  5. #5
    Unregistered
    Guest

    Too late a reply... but..

    PRELUDE
    >so how do i restrict this to only 3 digits after the decimal?
    If you know how to use floating point values correctly, then you shouldn't have to worry about extra precision that you don't need.
    When I asked a question about precision, your answer was that I don't need to know it..

    Is there anyone here who would like to "help" me with
    my question on restricting the digits on floats?

    sorry to say this but ..Prelude --- Just don't say anything if you don't want to answer ... that makes you one less than an Idiot.

  6. #6
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    Rounding? Why not use ceil() and floor() in math.h or cmath?

    sorry to say this but ..Prelude --- Just don't say anything if you don't want to answer ... that makes you one less than an Idiot.
    Sorry to say this, but Prelude answered your question. Don't cry because it wasn't handed to you on a silver platter.

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    sorry to say this but ..Prelude --- Just don't say anything if you don't want to answer ... that makes you one less than an Idiot.
    Don't bash well-respected members like that. You didn't read her post and then you insult her! You are the idiot!
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  8. #8
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    I asked a question along these lines sometime ago. I recall insisting that 10 divided by 10 multiplied by 10 is 10 by definition. So why was a getting trailing decimal inaccuricies?

    The problem is that the computer is working in base 2, where we work in base 10 (decimal). What is a rational fraction in decimal is not in base 2. Hence, when dealing with floating points, there may always be small inaccuracies due to rounding of irrational fractions.

    When dealing floating values, it is best not to worry about such rounding errors, in the same way you would round PI to 4 decimal places.

    However, if you need precise control over the thousandth digit, I would recommend using integers, where the integer value represents a thousandth. Therefore, a value of 500 would be one half. You can convert the integer back to a float whenever needed by dividing it by 1000.

  9. #9
    Unregistered
    Guest

    A Hole

    .......... you. He/She (Prelude) didn't answer my question.
    you shouldn't have to worry about extra precision that you don't need.
    My question about precision was never answered.
    If she's respected by the public, ask me if i care, 'cause
    i don't find one single reason to do so.
    And if you want to show your idiotic nature by turning against
    me, I can't really do anything but to say you are an a-hole.
    what's your problem man ? did i say anything wrong to you
    before?
    anyway, .......... you now!!! 'cause i don't need you ..........in replies.

  10. #10
    Prekool
    Guest

    I smell a fish...

    I am not trying to support anyone or anything here..
    but I think it's none of Sebastiani's business to interfere
    with what's going on.
    The moment he said "her" ... I was thinking... the general
    male tendency to impress a woman...
    anyway, whatever it is, he put his finger in the middle
    for no good reason... made an idiot of himself.

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >When I asked a question about precision, your answer was that I don't need to know it..
    My answer was that you can safely ignore it if you understand what you're doing and/or are aware of potential pitfalls.

    >so how do i restrict this to only 3 digits after the decimal ?
    I'll be more precise this time since last time I assumed you wanted a general answer. In the case of printf, you can adjust the precision that is printed out by saying:

    printf ( "%.3f", val );

    The actual value past the precision you want is irrelevant to your calculations.

    If you want to easily round a floating-point value to any precision, here's the function I was talking about which you obviously didn't bother to look for.
    Code:
    double round ( double value, unsigned decimal )
    {
      return ceil ( value * pow ( 10.0, decimal ) + .5 ) / pow ( 10.0, decimal );
    }
    >.......... you now!!! 'cause i don't need you ..........in replies.
    If you can't be civil then don't post. And please refrain from profanity no matter how censored, all those who frequent these boards are not adults. If you don't need our replies then don't start anymore threads, it makes no difference to us.

    >I think it's none of Sebastiani's business to interfere with what's going on.
    Agreed, nor is it yours. This is a forum for learning, not a flamers board and most certainly not a social group. If you want that then go to the General Discussions board, otherwise don't post unless you have something on-topic to add.

    -Prelude
    My best code is written with the delete key.

  12. #12
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    Originally posted by Davros
    I asked a question along these lines sometime ago. I recall insisting that 10 divided by 10 multiplied by 10 is 10 by definition. So why was a getting trailing decimal inaccuricies?

    The problem is that the computer is working in base 2, where we work in base 10 (decimal). What is a rational fraction in decimal is not in base 2. Hence, when dealing with floating points, there may always be small inaccuracies due to rounding of irrational fractions.

    When dealing floating values, it is best not to worry about such rounding errors, in the same way you would round PI to 4 decimal places.

    However, if you need precise control over the thousandth digit, I would recommend using integers, where the integer value represents a thousandth. Therefore, a value of 500 would be one half. You can convert the integer back to a float whenever needed by dividing it by 1000.
    to extend on what he said, there are floating point numbers, and fixed point numbers. fixed point numbers are essentially integers divided by 10 to some exponent. floating point numbers are different:

    2^e * (1+m)
    where e is the exponent, and 1+m is the mantissa. floats are done this way so they can be very flexible as to the size. in exchange, they give up some precision.

  13. #13
    Prekool
    Guest
    Prelude is an uncivilised ***** herself.

  14. #14
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Come on people, lets all act like adults here. To answer your question (despite rudeness) you may want to try something called fixed point math. For instance, a short is a 16 bit value. You can make the first 8 bits be a decimal value and the high 8 bits the integer part. That is just an example, because 8 bits is the size of a char thus only holding a value no greater than 255.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help to show decimals
    By engstudent363 in forum C Programming
    Replies: 4
    Last Post: 02-19-2008, 04:13 PM
  2. allow decimals
    By dankassdann in forum C++ Programming
    Replies: 3
    Last Post: 10-28-2006, 06:41 PM
  3. multiple decimals = problem
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 07-18-2002, 08:32 AM
  4. Decimals To Binary
    By kas2002 in forum C++ Programming
    Replies: 8
    Last Post: 06-08-2002, 11:12 AM
  5. Decimals to Fractions
    By ToasterPas in forum C++ Programming
    Replies: 4
    Last Post: 12-28-2001, 12:58 PM