Write your question here.
Hello! I am writing a bit of code, but the issue I run into is the that values are wrong! :(
I notice that it is not rounding up, I tried set precision, but it only works on Cout? What can I use for a function or a cin?
Thank you so much
If you want to round up and have a new value for the left side of the decimal point try "ceil" from the "cmath" header file. There is also "floor" for rounding down and "round" (C++11 on).
As I think you have found "serprecision" only works in the "cout" and only on the right side of the decimal point.
Line 37 may only ignore one character from the input buffer, but that may be all you need. I like to use: doTheCancan.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
You do not need line 38 because you also have it in line 50.
Actually after line 46 I would use std::cout << std::fixed << std::showpoint << std::setprecision(2); as you only need it once because it will affect everything after it until you change it.
Thank you Andy, But I was wondering how I would get it to round 2 decimal Places?
so in DataFile.txt I have:
Coin bank
11//Height
23.3//Circumference
After going through the function actualRadius I get a return Value of 3.35987261144649682
and PI is 3.1400000000000000001, giving me absurd numbers. I see that ceil and floor round to the nearest whole number, is there anything to get to two decimal places?
some values can't be represented exactly in float/double. The extra grillionth on pi is not a problem and its just the nature of floating point values. The general rule of thumb is to do everything in as high a precision as you can and then round once at the end, so your rounding isnt accumulated leading to bigger errors later. On that note you might want to use more digits on pi.
you can multiply by some power of 10, in your case, 100, and use floor/ceil with that, and divide back.
> After going through the function actualRadius I get a return Value of 3.35987261144649682
> and PI is 3.1400000000000000001, giving me absurd numbers.
Floating point values are inexact. For instance, the precise mathematical value of 3.14 may not be representable as a floating point number; 3.1400000000000001 (to 16 digits after the decimal) may be the closest approximation that is available.
Just round the result to 2 decimal places in the output.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <iostream>
#include <cmath>
#include <iomanip>
int main()
{
std::cout << "representable floating point values around 3.14\n"
<< "(double, to 16 digits after the decimal point)\n" ;
double n = 3.139999999999995 ;
for( int i = 0 ; i < 25 ; ++i )
{
std::cout << std::fixed << std::setprecision(16) << n << '\n' ;
// http://en.cppreference.com/w/cpp/numeric/math/nextafter
n = std::nextafter( n, 100.0 ) ;
}
}
representable floating point values around 3.14
(double, to 16 digits after the decimal point)
3.1399999999999948
3.1399999999999952
3.1399999999999957
3.1399999999999961
3.1399999999999966
3.1399999999999970
3.1399999999999975
3.1399999999999979
3.1399999999999983
3.1399999999999988
3.1399999999999992
3.1399999999999997
3.1400000000000001
3.1400000000000006
3.1400000000000010
3.1400000000000015
3.1400000000000019
3.1400000000000023
3.1400000000000028
3.1400000000000032
3.1400000000000037
3.1400000000000041
3.1400000000000046
3.1400000000000050
3.1400000000000055