Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/utils/DoubleUtil.java @ 1190:f514894ec2fd
merged flys-artifacts/2.5
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:14:17 +0200 |
parents | 913b52064449 |
children | 09c1292cf36d |
comparison
equal
deleted
inserted
replaced
917:b48c36076e17 | 1190:f514894ec2fd |
---|---|
1 package de.intevation.flys.utils; | |
2 | |
3 public class DoubleUtil | |
4 { | |
5 public static final double DEFAULT_STEP_PRECISION = 1e6; | |
6 | |
7 private DoubleUtil() { | |
8 } | |
9 | |
10 public static final double [] explode(double from, double to, double step) { | |
11 return explode(from, to, step, DEFAULT_STEP_PRECISION); | |
12 } | |
13 | |
14 public static final double round(double x, double precision) { | |
15 return Math.round(x * precision)/precision; | |
16 } | |
17 | |
18 public static final double round(double x) { | |
19 return Math.round(x * DEFAULT_STEP_PRECISION)/DEFAULT_STEP_PRECISION; | |
20 } | |
21 | |
22 public static final double [] explode( | |
23 double from, | |
24 double to, | |
25 double step, | |
26 double precision | |
27 ) { | |
28 double lower = from; | |
29 | |
30 double diff = to - from; | |
31 double tmp = diff / step; | |
32 int num = (int)Math.abs(Math.ceil(tmp)) + 1; | |
33 | |
34 double [] values = new double[num]; | |
35 | |
36 if (from > to) { | |
37 step = -step; | |
38 } | |
39 | |
40 for (int idx = 0; idx < num; idx++) { | |
41 values[idx] = round(lower, precision); | |
42 lower += step; | |
43 } | |
44 | |
45 return values; | |
46 } | |
47 } | |
48 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |