How to calculate the sum of squares of an array of floating-point values in Java

1 Answer

0 votes
import java.util.Arrays;

public class Program {
    public static void main(String[] args) {
        double[] arr = { 0.05, 0.86, 0.001, -0.29, -4.81 };

        double sumOfSquares = Arrays.stream(arr)
                                    .map(x -> x * x)
                                    .sum();

        System.out.println(sumOfSquares);
    }
}



/*
run:

23.962300999999997

*/

 



answered Jun 29, 2025 by avibootz
...