0

I have given a log value Y , i want to calculate the anti log of Y i.e

ans = (Math.pow(10,Y))%mod

where mod = 1e9+7 and the anti log of Y will always be integer i.e
Y is calculate as follow Y= log(a) a is very large integer of range 10^100000

So for given Y i need to calculate ans ? How to do that considering the mod operation.

My Approach

double D = Y -(int)Y
long Pow = (long)Y

  for(int i=1;i<=Pow;i++) ans = (ans*10)%mod;

  ans = (ans*Math.pow(10,D))%mod

But it's not correct can someone suggest be efficient approach here ? BigDecimal can be useful there ?
For Example:

Y = 16.222122660468525

Using the straight forward method and rounding off i.e Math.log(10,Y) give me 1667718169966651 but using loops it's give me 16677181699666510. I am not using mod now just explaining that there is an error.

Here Y is small so direct method works and we can take mod easily. if Y is range of 10000 it will not work and overflow so we have to used mod.

Regression
  • 31
  • 5

2 Answers2

0

I guess it's should work

double D = Y -(int)Y
long Pow = (long)Y

for(int i=1;i<=Pow;i++) ans = (ans*10)%mod;

 ans = (ans*Math.pow(10,D))
 ans = Math.round(ans)
 ans%=mod
Narendra Modi
  • 803
  • 1
  • 9
  • 29
0

There is an error in your judgement here - the loop method is not at fault.

The value of a in your example has 17 integer digits. From this stackoverflow post, a double has ~16 significant digits of precision. Thus both the loop and direct calculations are in fact being limited by lack of precision.

(Just to confirm, using a high precision calculator, the value of a is 16677181699666650.8689546562984070600381634077.... Thus both of your values are incorrect - unless you copied them wrongly?)

Thus your loop method is not the problem; you just need a higher-precision method to do the last step (calculating pow(10, frac(Y))).


As a side note, there is a more efficient way of doing the loop part - this post has more details.

meowgoesthedog
  • 13,415
  • 4
  • 20
  • 34