Answer the question
In order to leave comments, you need to log in
How to find the mantissa of a decimal number?
What are the ways to find the mantissa of a number? In addition to dividing by 10 in a cycle and the decimal logarithm.
I would like to find an elegant and fast working solution.
Answer the question
In order to leave comments, you need to log in
Here "elegant" and "fast" are slightly contradictory requirements. The fastest way is to determine the number of characters by dividing in half for 3-4 comparisons:
double mantissa(int c){
int a=abs(c);
int b=1;
if(a>=100000){
if(a>=10000000){
if(a>=1000000000) b=1000000000;
else if(a>=100000000) b=100000000;
else a=10000000;
}else if(a>=1000000) b=1000000;
else b=100000;
}else{
if(a>=100){
if(a>=10000) b=10000;
else if(a>=1000) b=1000;
else a=100;
}else if(a>=10) b=10;
}
return (double)c/b;
}
double mantissa(int a){
if(a==0) return 0;
float f=a;
int b=((*(int*)&f&0x7fffffff)-0x40cd3ed7)/0x019a7daf;
double d=a*pow(0.1,b);
if(fabs(d)>=10) d/=10;
return d;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question