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

Thank you,

R

Printable View

- 07-08-2002RyeDunndecimals
Can someone help me get started, or show me an example of how to allow a decimal (but only one) in an integer? :confused:

Thank you,

R - 07-08-2002Xmevs
float?

Code:`float f = 4.0`

int i = 4

f != i

- 07-08-2002Unregisteredi 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 ? - 07-08-2002Prelude
>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 - 07-14-2002UnregisteredToo late a reply... but..Quote:

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. - 07-14-2002Dual-Catfish
Rounding? Why not use ceil() and floor() in math.h or cmath?

Quote:

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.

- 07-14-2002SebastianiQuote:

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.

- 07-14-2002Davros
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. - 07-14-2002UnregisteredA Hole
.......... you. He/She (Prelude) didn't answer my question.

Quote:

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. - 07-14-2002PrekoolI 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. - 07-14-2002Prelude
>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 );

}

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 - 07-14-2002ygfpersonQuote:

*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. - 07-14-2002Prekool
Prelude is an uncivilised ***** herself.

- 07-15-2002master5001
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.