i'm trying create function able calculate checksum of 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 little code, calculates checksum of 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 improve it, works floating point numbers?
background
i try find out if true pattern example continue forever result of 9 checksum.
update
unfortunattely c++ not precise enough project. e.g. if calculate 0.703125 / 2
result 0.3515625
, in c++ result 0.351563
. code: http://www.pasteall.org/61345/cpp
let want calculate checksum upto 5 decimal point of floating point number multiply floating point number 100000 , take 'floor' calculate checksum function.
edit
as float , double has precession problem it's better not use them calculate checksum more digit(15 digit double). more digit use string representation of floating number.
Comments
Post a Comment