mr AHR
شنبه 16 دی 1391, 02:04 صبح
این کد اومدیم به یکی از دوستان کمک کنیم خودمون گیر افتادیم
چرا اینجا floor ، مثل آدم کار نمیکنه inf برمیگردونه ؟؟؟
این برنامه قراره عدد رو گرد کنه
منطقاً درسته ، ولی عملا ایراد داره !
اینقدر بدم میاد نمیفهمم یه وقت هایی سطح پایین چه اتفاقی میفته !!!
#include<iostream>
#include<cmath>
using namespace std;
int main() {
double toRound = 0; // Varaible to round
unsigned int precision = 0; // Precision to be kept
double result = 0; // Keeps the final result
// Reading inputs . For simplicity we discard input failure .
cout << "Enter number to round : ";
cin >> toRound;
cout << "Enter Precision to be kept : ";
cin >> precision;
toRound *= pow(10,precision);
/*
* Consider a number say 90.2582 we want to keep 2 decimal part & discard others .
* 90.2583 * 10^2 = 9025.83
* Now we can safety remove all of the decimal parts => floor(9025.83) = 9025
* Now we put the decimal point in the correct place by this :
* 9025 * 10^-2 = 90.25 <- the number without that extra parts .
*/
double toBeKept = double( floor(toRound * pow(10,precision)) ) * pow(10,-precision);
double toDiscard = toBeKept - toRound;
if ( toDiscard > 0.5 ) {
result = toBeKept + pow(10,precision); // Round up
}
else {
result = toBeKept; // Round down
}
cout << "The result is : " << result;
cin.ignore();
cin.get();
}
چرا اینجا floor ، مثل آدم کار نمیکنه inf برمیگردونه ؟؟؟
این برنامه قراره عدد رو گرد کنه
منطقاً درسته ، ولی عملا ایراد داره !
اینقدر بدم میاد نمیفهمم یه وقت هایی سطح پایین چه اتفاقی میفته !!!
#include<iostream>
#include<cmath>
using namespace std;
int main() {
double toRound = 0; // Varaible to round
unsigned int precision = 0; // Precision to be kept
double result = 0; // Keeps the final result
// Reading inputs . For simplicity we discard input failure .
cout << "Enter number to round : ";
cin >> toRound;
cout << "Enter Precision to be kept : ";
cin >> precision;
toRound *= pow(10,precision);
/*
* Consider a number say 90.2582 we want to keep 2 decimal part & discard others .
* 90.2583 * 10^2 = 9025.83
* Now we can safety remove all of the decimal parts => floor(9025.83) = 9025
* Now we put the decimal point in the correct place by this :
* 9025 * 10^-2 = 90.25 <- the number without that extra parts .
*/
double toBeKept = double( floor(toRound * pow(10,precision)) ) * pow(10,-precision);
double toDiscard = toBeKept - toRound;
if ( toDiscard > 0.5 ) {
result = toBeKept + pow(10,precision); // Round up
}
else {
result = toBeKept; // Round down
}
cout << "The result is : " << result;
cin.ignore();
cin.get();
}