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
This is a discussion on decimals within the C++ Programming forums, part of the General Programming Boards category; Can someone help me get started, or show me an example of how to allow a decimal (but only one) ...
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
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 ?
>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.
When I asked a question about precision, your answer was that I don't need to know it..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.
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.
Rounding? Why not use ceil() and floor() in math.h or cmath?
Sorry to say this, but Prelude answered your question. Don't cry because it wasn't handed to you on a silver platter.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!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.
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; }
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.
.......... you. He/She (Prelude) didn't answer my question.
My question about precision was never answered.you shouldn't have to worry about extra precision that you don't need.
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.
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.
>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.
>.......... you now!!! 'cause i don't need you ..........in replies.Code:double round ( double value, unsigned decimal ) { return ceil ( value * pow ( 10.0, decimal ) + .5 ) / pow ( 10.0, decimal ); }
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.
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: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.
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.
Prelude is an uncivilised ***** herself.
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.