Reputation: 20232
I'm trying to create a function which is able to calculate the checksum of any number, including floating point numbers.
For example:
360° = 3+6+0 = 9
180° = 1+8+0 = 9
90° = 9+0 = 9
45° = 4+5 = 9
22.5° = 2+2+5 = 9
11.25° = 1+1+2+5 = 9
5.625° = 5+6+2+5 = 18 = 1+8 = 9
2.8125° = 2+8+1+2+5 = 18 = 1+8 = 9
1.40625 = 1+4+0+6+2+5 = 18 = 1+8 = 9
0.703125 = 0+7+0+3+1+2+5 = 18 = 1+8 = 9
0.3515625 = 0+3+5+1+5+6+2+5 = 27 = 2+7 = 9
0.17578125 = 0+1+7+5+7+8+1+2+5 = 36 = 3+6 = 9
...
I wrote this little code, which calculates the checksum of an Integer:
#include<iostream>
using namespace std;
int checksum(int param)
{
int sum = 0;
while (param > 0)
{
sum += param % 10;
param /= 10;
}
while (sum > 9) { sum = checksum(sum); }
return sum;
}
int main()
{
int number = 0;
cout<<"Enter number:"<<endl;
cin>> number;
cout<< checksum(number);
cin.get(); cin.get();
return 0;
}
How can I improve it, so that it also works with floating point numbers?
Background
I try to find out if it is true that the pattern from my example will continue forever with the result of 9 as checksum.
Update
Unfortunattely C++ is not precise enough for this project. E.g. if i calculate 0.703125 / 2
then the result will be 0.3515625
, but in C++ the result is 0.351563
.
My Code: http://www.pasteall.org/61345/cpp
Upvotes: 3
Views: 1288
Reputation: 20232
I was able to solve it by myselve.
Input: 1.97654
Output: The checksum of 1.97654 is 5
#include<iostream>
#include<sstream>
#include<string>
#include<math.h> /* pow */
using namespace std;
int checksum(int param)
{
int sum = 0;
while (param > 0)
{
int r1 = param % 10;
sum += r1;
param /= 10;
}
while (sum > 9) { sum = checksum(sum); }
return sum;
}
int main()
{
string number;
cout<<"Enter number:"<<endl;
cin>> number;
double param_as_double = stod(number);
int front_part = stoi(number);
double after_point_part = param_as_double - front_part;
ostringstream strs;
strs << after_point_part;
string after_point_part_str = strs.str();
int nachkomma_len = after_point_part_str.length()-1;
after_point_part *= pow(10.00,nachkomma_len-1);
strs.str("");
strs << after_point_part;
after_point_part_str = strs.str();
int after_point_part_int = stoi(after_point_part_str);
int SUM = 0;
SUM = checksum(front_part);
SUM += checksum(after_point_part_int);
SUM = checksum(SUM);
cout<<"The checksum of "<<number<<" is "<< SUM <<endl;
cin.get(); cin.get();
return 0;
}
Probably it is more code then necessary, but i am tired and therefor too lazy to improve it. I did not had enough time to test it for bugs yet, so im not sure if it is working for 100%.
Upvotes: 0
Reputation: 16318
I never heard of a checksum on real numbers, by summing the digits. You should take the binary representation, 4 or 8 bytes, and sum those. That would make actual sense.
Upvotes: 1
Reputation: 5741
Let say you want to calculate checksum upto 5 decimal point of floating point number then simply multiply your floating point number with 100000 and take 'floor' then calculate checksum with your function.
EDIT
As float and double has precession problem it's better not to use them to calculate checksum for more digit(15 digit for double). For more digit use string representation of floating number.
Upvotes: 2