Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 rounding float value
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
sockoo




PostPosted: Thu Feb 08, 2007 7:53 pm   Post subject: rounding float value

i need to round a float value.

i have an assignment where i am required to paint a room. but depending on the dimensions of the room i may end up with lets say 200.4 cans of paint required.

is there a way i could figure out if the decimal point is < than 0 than round up, else !< 0 do nothing.

i thought of using the % , but soon realized i was way on the wrong track.

i also thought of converting the float value into an INT value, but than i would loose the whole decimal point and not give correct values or numbers.

i could also convert to int and than add one but than if the number is even and i add one its not giving me a good result.

ex:

200 + 1 = 200

it should be 200, there is no decimal.

What i need

Ex:

200.4531
Oh the decimal is < than 0
201 cans is needed.

I hope some one is able to help me or give me advice.

Thank you in advance and i hope i have supplied enough information.
Sponsor
Sponsor
Sponsor
sponsor
ericfourfour




PostPosted: Thu Feb 08, 2007 8:58 pm   Post subject: RE:rounding float value

Do you mean a function that rounds up? I'm sure there is a ceil function in one libraries. Try math. Search the msdn if you can't find it.
sockoo




PostPosted: Fri Feb 09, 2007 10:25 am   Post subject: Re: rounding float value

i'v heard of ceil, the only problem is my teacher doesnt want us to go above and beyond what he hasnt taught us.

therefore he hasnt taught us ceil so i cannot use it.

i was more refering to some odd or obscure way i could figure it out. But i guess there is no way to figure that out except for using the ciel function or unless someone else knows of another way

please post anything, all and any help is greatly appriciated.

thank you
wtd




PostPosted: Fri Feb 09, 2007 2:14 pm   Post subject: RE:rounding float value

In that case, without knowing exactly what you have learned, we cannot help you.
r.3volved




PostPosted: Fri Feb 09, 2007 2:32 pm   Post subject: RE:rounding float value

Look into the math.h library
...Specificly round, ceiling and floor

Programming is all about going above and beond what you already know. Whats the point in coding if you're just solving a problem that's already been given a solution?

MSDN is a great resource for standard library definitions and examples (though their examples tend to be poor at best) for the whole Visual Studio suite.

Programming is about expanding your knowledgebase and being able to solve problems on your own without relying solely on what's been taught to you. Use the basis of your teachings as a stepping stone into self-learning.
OneOffDriveByPoster




PostPosted: Fri Feb 09, 2007 2:50 pm   Post subject: Re: RE:rounding float value

r.3volved @ Fri Feb 09, 2007 2:32 pm wrote:
Programming is all about going above and beond what you already know. Whats the point in coding if you're just solving a problem that's already been given a solution?

You can also learn by figuring out the solution from the tools you already have.

Hint: you can compare the float value and the truncated value.
abcdefghijklmnopqrstuvwxy




PostPosted: Fri Feb 09, 2007 3:19 pm   Post subject: RE:rounding float value

Can I just ask what you mean by the decimal is < 0?

Here you hae 2.4 which you want to be 3. What if it was 2.6, would it still be 3? So in that case if there is ever a decimal you automatically round up?

The easiest way to do that is to put it in a string and compare say
string1("0")
string2("2.345")
for (int i=0; i < string2.length(); i++)
{
if (string2[i].equals(".")
{
i++;
if (string2[i].equals("0"))
//etc....
}
}

EDIT: actually that is not the easiest way but it is a way that can work.
ericfourfour




PostPosted: Fri Feb 09, 2007 3:28 pm   Post subject: RE:rounding float value

This is exactly what he is looking for: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_CRT_ceil.asp.
Sponsor
Sponsor
Sponsor
sponsor
ownageprince




PostPosted: Fri Feb 09, 2007 7:22 pm   Post subject: Re: rounding float value

int Round(float x)
/*Returns x rounded to the nearest integer
Pre:x>=0
Post: rounded x returned */
{
float FractionalPart=x-int(x);
if(FractionalPart<0.5)
return(int(x));
else
return(int(x)+1);
}

//just replaced double with float...i think it should it
wtd




PostPosted: Fri Feb 09, 2007 8:42 pm   Post subject: RE:rounding float value

Consider how many times you call "int(x)". Can ou streamline this?

Oh, and use code tags!
haskell




PostPosted: Fri Feb 09, 2007 9:56 pm   Post subject: RE:rounding float value

Dum da da dum dum! Google to the rescue.

C++ Reference Guid > Numerical Rounding

Quote:
Rounding Down

This trivial rounding policy simply discards the fraction part of a floating point value, keeping only the integral part of the original number. For example, the numbers 11.0, 11.3 and 11.99 are all rounded down to 11.0. If your app deals with floating point values exclusively, you may use the floor() function declared in <cmath> for this purpose:

code:

#include <cmath>

double floor(double val);
long double floorl(long double val);


floor() returns the largest integer not greater than val. floorl() is the long double version of this function. The following code uses floor() to round down a number:

code:
double result=floor(11.99); //result=11.0


Obviously, you don’t need to use floor() or floorl() if you convert floating point numbers to integral types because this implicit conversion automatically truncates the result, discarding the fractional part:
code:
double val=11.99;
long result=val; //implicit truncation



Quote:
Rounding Up

The opposite rounding policy rounds up any fraction that’s bigger than zero. For example, the numbers 11.3 and 11.99 are rounded up to 12.0 whereas 11.0 remains 11.0. When is this type of rounding used? Suppose that maximum the number of passenger in one carriage is 40, and you need to drive 100 passengers in the same train. The number of carriages must be rounded up the closest integer not smaller than 100/40. Use the ceil() function in such cases:

code]#include <cmath>
double ceil(double val);
long double ceill(long double val);[/code]

ceil() finds the smallest integer not less than val.

code:
const int PASSENGERS_PER_CARRIAGE=40;
int passengers=100;
int carriages = ceil ((double) passengers/ PASSENGERS_PER_CARRIAGE);//3


Quote:
Rounding To the Nearest Value

A more common type of rounding rounds the result to the nearest integral value. If the fraction part is smaller than 0.5, the fraction is discarded, meaning the number is rounded down. Otherwise, it’s rounded up. The Standard Library doesn’t define a function for this type of rounding, so you have to implement it by yourself (technically speaking, there is a standard facility for controlling rounding types but in practice, most implementations use a hard-coded rounding policy so you can’t really override the default settings of your implementation). The simplest method for rounding a floating point value to the nearest number is to add 0.5 to the original value and then truncate the result:

code:
double v1=11.2, v2=11.99;
double near_rounded=floor(v1+.5); //11.0
near_rounded=floor(v2+.5); //12

If you’re dealing with integral types, you don’t need floor(). Adding the constant 0.5 to the original floating point value and then assigning the result to an integral variable will produce the desired effect:
code:
double v1=11.2, v2=11.99;
long near_rounded=v1+.5; //11.0
near_rounded=v2+.5; //12


You may have noticed that this technique has a bug, though. It doesn’t produce the correct results for negative values. When negative values are involved, you have to deduct 0.5 from the original value:

code:
//buggy code: adds .5 instead of subtracting .5
double v1=-11.2, v2=-11.99;
long near_rounded=v1+.5; //oops! -10 instead -11
near_rounded=v2+.5; //oops! -11instead of -12


Switching between +0.5 and -0.5 is inelegant and confusing. Instead, it’s best to let a function return either a positive or negative constant according to the sign of the original value. For example, if the original value is -11.2, the function returns the constant -0.5 or 0.5 otherwise:

code:
double get_factor (double val)
{
     return val<0 ?-0.5 : 0.5;
}


Now all you have to do is add the factor to the original value:

code:
double v1=-11.2, v2=-11.99;
long near_rounded=get_factor(v1)+v1; //11.0
near_rounded=get_factor(v2)+v2; //12.0

Or if you need to use floating point datatypes:

code:
double v1=-11.2, v2=-11.99;
double near_rounded=long(get_factor(v1)+v1); //11.0
near_rounded=long(get_factor(v2)+v2); //12.0

In case you’re wondering why I didn’t use floor() to truncate the result before assigning it to near_rounded, it’s because floor() would lead to an incorrect result. Consider:

code:
floor(get_factor(v2)+v1);


is equivalent to floor(-11.7) which returns -12. However, the correct result of rounding the negative number -11.2 is -11. What’s going on here? Recall that floor() returns "the largest integer not greater than val". When dealing with negative numbers, the largest integer not greater than -11.2 is -12, because -12 is smaller (i.e. not greater) than -11.2.


I'm pretty sure that this is what you're looking for.
Cinjection




PostPosted: Mon Mar 05, 2007 3:54 pm   Post subject: Re: rounding float value

Wouldn't it be easier to add 0.5 to the float value and then trunecate the decimal? Is that what you need? I'm a little confused. If you have 2.4 and it should be 2, you wouyld get 2.4 + 0.5 = 2.9 (trunicated is 2). If it's 2.6, then 2.6 + 0.5 = 3.1 (trunicated is 3)

Is that the kind of rounding you ment? I hope this helps.
ericfourfour




PostPosted: Mon Mar 05, 2007 5:07 pm   Post subject: RE:rounding float value

Now that you figured out how to round, can you think of an algorithm that only rounds up (ceil)?

Ex. ceil(2) == 2, ceil(2.1) == 3, ceil(2.6) == 3
klopyrev




PostPosted: Mon Mar 05, 2007 5:36 pm   Post subject: Re: rounding float value

For ceil, I've personally always used (int)(number+0.99999999), but I don't think that's a good method. What does the standard library in C use?

KL
Cinjection




PostPosted: Mon Mar 05, 2007 5:49 pm   Post subject: Re: RE:rounding float value

ericfourfour @ Mon Mar 05, 2007 5:07 pm wrote:
Now that you figured out how to round, can you think of an algorithm that only rounds up (ceil)?

Ex. ceil(2) == 2, ceil(2.1) == 3, ceil(2.6) == 3


Yup sorry about that. How about the ceil command in C++'s math.h

roundedNumber = ceil(float numberToRound);
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 15 Posts ]
Jump to:   


Style:  
Search: