Tuesday, 15 January 2013

Math - mapping numbers java -



Math - mapping numbers java -

i saw this question of mapping numbers algorithm.

i trying implement @peterallenwebb solution in java this:

long = 1l; long b = 999999999l; long c = 1000000000l; long d = 9999999999l; long x = 999999998l; long y = (d-c)*(x-a)/(b-a) + c; system.out.println("original " + x); long reversex = (b-a)*(y-c)/(d-c) + a; system.out.println("reverse " + reversex);

however, doesn't work.

see below:

x reversex 999999998 999999997 1 1 999999999 999999999 12 11

as can see, minimum (a) , maximum (b) returning fine.

for rest, need add together 1. seems me floor/round/math issue , don't want rely on jvm calculates it. work.

how can create above work reversex?

you facing age old issue. default partition double in java. if partition result 1.0 or 1.3 or 1.9 truncated 1. in case same happening. seek changing double long below

double = 1l; double b = 999999999l; double c = 1000000000l; double d = 9999999999l; double x = 999999998l; double y = (d - c) * (x - a) / (b - a) + c; system.out.println("original " + new decimalformat("#").format(x)); double reversex = (b - a) * (y - c) / (d - c) + a; system.out.println("reverse " + new decimalformat("#").format(reversex));

java math

No comments:

Post a Comment