Getting double decimal

I work with the cos value, which is represented in double. Thus, my borders are between -1.0 and 1.0, however for my work I just ignore all negative variables.

Now I would like to get the decimal number of this double number (sorry I could not find the literary term for this operation)

Mostly with examples:

Suppose the input is 0.12, then it can be written as 1.2 * 10 ^ -1, what I expect to get is just the part where it is 10 ^ -1

Another example is 0.00351, which can be written as 3.51 * 10 ^ -3, so the expected result is 10 ^ -3

I developed this algorithm below, but it’s kind of fast and dirty. I was wondering if there was any math trick to avoid using a loop.

double result = 1;
while (input < 1.0) {
   input *= 10.0;
   result /= 10.0;
}

, 0.

Java , .

+4
3

, 10 - Math.log10,

 Math.log10(input)

.

log10(100) = 2
log10(1e-5) = -5

.

, (10 )

+3
public class Tester {

    public static double lowbase(double v) {
        return Math.pow(10, Math.floor(Math.log10(Math.abs(v))));
    }   

    public static void main(String [] args){
        System.out.println(lowbase(0.12));
        System.out.println(lowbase(0.00351));
        System.out.println(lowbase(0));
        System.out.println(lowbase(-1));
    }   

}

:

0.1
0.001
0.0
1.0

abs , .

+3

, "10^-2" ( , ).

public String getCientific(double input){
    int exp;
    exp = (int) java.lang.Math.floor(java.lang.Math.log10(input));
    return  "10^" + exp;
 }
+1

Source: https://habr.com/ru/post/1543005/


All Articles