Home > OS >  FASTEST way to truncate a float in Java
FASTEST way to truncate a float in Java

Time:10-17

I have a program that takes in anywhere from 20,000 to 500,000 velocity vectors and must output these vectors multiplied by some scalar. The program allows the user to set a variable accuracy, which is basically just how many decimal places to truncate to in the calculations. The program is quite slow at the moment, and I discovered that it's not because of multiplying a lot of numbers, it's because of the method I'm using to truncate floating point values.

I've already looked at several solutions on here for truncating decimals, like this one, and they mostly recommend DecimalFormat. This works great for formatting decimals once or twice to print nice user output, but is far too slow for hundreds of thousands of truncations that need to happen in a few seconds.

What is the most efficient way to truncate a floating-point value to n number of places, keeping execution time at utmost priority? I do not care whatsoever about resource usage, convention, or use of external libraries. Just whatever gets the job done the fastest.

EDIT: Sorry, I guess I should have been more clear. Here's a very simplified version of what I'm trying to illustrate:

import java.util.*;
import java.lang.*;
import java.text.DecimalFormat;
import java.math.RoundingMode;
public class MyClass {
    
    static class Vector{
        float x, y, z;
        @Override
        public String toString(){
            return "["   x   ", "   y   ", "   z   "]";
        }
    }
    
    public static ArrayList<Vector> generateRandomVecs(){
      ArrayList<Vector> vecs = new ArrayList<>();
      Random rand = new Random();
      for(int i = 0; i < 500000; i  ){
          Vector v = new Vector();
          v.x = rand.nextFloat() * 10;
          v.y = rand.nextFloat() * 10;
          v.z = rand.nextFloat() * 10;
          vecs.add(v);
      }
      return vecs;
    }
    
    public static void main(String args[]) {
        
    int precision = 2;
    
    float scalarToMultiplyBy = 4.0f;
   
     ArrayList<Vector> velocities = generateRandomVecs();
     
     System.out.println("First 10 raw vectors:");
     for(int i = 0; i < 10; i  ){
         System.out.print(velocities.get(i)   " ");
     }
      
     /* 
     This is the code that I am concerned about
     */
     
     DecimalFormat df = new DecimalFormat("##.##");
     df.setRoundingMode(RoundingMode.DOWN);

     long start = System.currentTimeMillis();

     for(Vector v : velocities){
        /* Highly inefficient way of truncating*/
        v.x = Float.parseFloat(df.format(v.x * scalarToMultiplyBy));
        v.y = Float.parseFloat(df.format(v.y * scalarToMultiplyBy));
        v.z = Float.parseFloat(df.format(v.z * scalarToMultiplyBy));
     }
      
     long finish = System.currentTimeMillis();
     long timeElapsed = finish - start;
     
     System.out.println();
     System.out.println("Runtime: "   timeElapsed   " ms");
     
     System.out.println("First 10 multiplied and truncated vectors:");
     for(int i = 0; i < 10; i  ){
         System.out.print(velocities.get(i)   " ");
     }
    }
}

The reason it is very important to do this is because a different part of the program will store trigonometric values in a lookup table. The lookup table will be generated to n places beforehand, so any velocity vector that has a float value to 7 places (i.e. 5.2387471) must be truncated to n places before lookup. Truncation is needed instead of rounding because in the context of this program, it is OK if a vector is slightly less than its true value, but not greater.

Lookup table for 2 decimal places:
...
8.03 -> -0.17511085919
8.04 -> -0.18494742685
8.05 -> -0.19476549993
8.06 -> -0.20456409661
8.07 -> -0.21434223706
...

Say I wanted to look up the cosines of each element in the vector {8.040844, 8.05813164, 8.065688} in the table above. Obviously, I can't look up these values directly, but I can look up {8.04, 8.05, 8.06} in the table.

What I need is a very fast method to go from {8.040844, 8.05813164, 8.065688} to {8.04, 8.05, 8.06}

CodePudding user response:

The fastest way, which will introduce rounding error, is going to be to multiply by 10^n, call Math.rint, and to divide by 10^n.

That's...not really all that helpful, though, considering the introduced error, and -- more importantly -- that it doesn't actually buy anything. Why drop decimal points if it doesn't improve efficiency or anything? If it's about making the values shorter for display or the like, truncate then, but until then, your program will run as fast as possible if you just use full float precision.

CodePudding user response:

When decimal precision is important, you should be using BigDecimal .

Changing the following in your code makes it considerably faster.

    static class Vector{
        BigDecimal x, y, z;
        ...
    }
    ...
    public static ArrayList<Vector> generateRandomVecs(){
        ArrayList<Vector> vecs = new ArrayList<>();
        Random rand = new Random();
        for(int i = 0; i < 500000; i  ){
            Vector v = new Vector();
            v.x = BigDecimal.valueOf(rand.nextFloat() * 10);
            v.y = BigDecimal.valueOf(rand.nextFloat() * 10);
            v.z = BigDecimal.valueOf(rand.nextFloat() * 10);
            vecs.add(v);
        }
        return vecs;
    }
    ... 
    public static void main(String args[]) {
        ...
        for(Vector v : velocities){
            v.x = v.x.setScale(precision, RoundingMode.DOWN);
            v.y = v.y.setScale(precision, RoundingMode.DOWN);
            v.z = v.z.setScale(precision, RoundingMode.DOWN);
        }
        ...
    }

  • Related