简体   繁体   中英

Could anyone help me on solve an algorithm? (Set a weight of a big range value)

I was wondering how to set a weight for a computation of huge different values. I will try to explain it better, or maybe in details.

The idea is: I would like to have as a result a value in this range [1,20] given: 1) first value: a 1a) with a format like 0.1 or 0.0001 or 0.00001 etc. 1b) maximum decimal length = 5 decimals 2) second value: b 2a) assumption that is less than a (if a=0.01, at least b=0.00999 etc.) 2b) with the same format 2c) maximum decimal length = 6 decimals (so 1 grater than a)

where, given for example a=0.01 and b=0.0001

x = a/b = 100

a second example a=0.01 and b=0.00001

x = a/b = 1000

I thought at something like this: If maximum could be (when a=0.1 and b=0.000001) = 100000 than it correspond to 20 (the new maximum scale) and if I count the difference of number of decimals I can say that: 'a' has 1 decimal 'b' has 6 decimal In general we can have a maximum difference of 5, so:

max = 5

So using this calculus:

diff=6-1=5

in order to correspond their fraction (a/b) as the maximum value (20) I can do: (diff*20)/max give me 20. That's ok. Now, If I have a=0.1 and b=0.000002 I can use the least significant number of 'b' (in this case '2' of 0.000002) and compare with the least significant of 'a', and I can say that

diff=6-1=5

Now subtract 0.2 (because the least significant number was 2) to have 4.8. Now recompute:

4.8*20/max = 19.2

So for 0.1 and 0.000003 would be

diff=6-1=5
diff=diff-0.3 = 4.7
4.7*20/max = 18.8

And so on.

The problem is that when I have a=0.3 and b=0.0002 (for example) I don't know how to manage it.

Could anyone suggest me a solution or a bunch of that even with a pseudo-code or procedure? Maybe I will do the translation in java by myself.

I don't know where to post my question, if so I really apologize if I wrong some rules of these website, otherwise everything is fine :-) Thank you all

Have you looked at logarithms?

 d = (log(a) - log(b))/log(10)

should give the answer. In other words, log(a)/log(10) gives you

0.1   -->  -1
0.01  -->  -2
0.001 -->  -3 

etc. So, you can calculate differences the way you want. And in the end,

exp(d*log(10))

is the actual ratio. In java, use Math.log()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM