0

I am working on chicken swarm optimization using Java and I am a little bit recently learner of Java.

In the question below there are a small amount added to the equation of rooster position update to overcome the problem of division of zero.

In Matlab there is something called realmin which returns the smallest positive normalized floating-point number in IEEE® double precision.

Is there anything equivalent to this in java? I don't want to use exceptions.

tempSigma = Math.exp( (fitSortand_Index [anotherRooster ][1]  
        - fitSortand_Index [i][1]) / (Math.abs( fitSortand_Index[i][1]) + realmin ) );
andrewJames
  • 19,570
  • 8
  • 19
  • 51
Dina
  • 1
  • 1
  • https://docs.oracle.com/javase/7/docs/api/java/lang/Double.html#MIN_NORMAL – Iłya Bursov Sep 18 '22 at 22:29
  • See if [this answer](https://stackoverflow.com/a/3884879/2055998) helps. – PM 77-1 Sep 18 '22 at 22:32
  • Thank you it is much helpful – Dina Sep 18 '22 at 22:36
  • No need to replicate that truck. This is better:`if Math.abs(fitSortand_Index[i][1]) < 1e-6 then tempSigma = 0`. – Cris Luengo Sep 18 '22 at 22:50
  • @CrisLuengo please would you explain to me why ? I mean 1e-6 ..thanks in advance – Dina Sep 22 '22 at 21:00
  • Never mind. I think I misread the operation. [`realmin` is equal to `2^(-1022)`](https://www.mathworks.com/help/matlab/ref/realmin.html), which is about 2.225e-308. Just use that as a constant in your code. The exact value doesn't matter, I guess. But dividing any value by such a small quantity will yield a large number. `exp(.)` of a large number is likely to overflow, the result of the operation would likely be infinity, depending on the value of `fitSortand_Index [anotherRooster ][1]`, which could be 0 as well, I guess. I think this is a weird operation. What is the original MATLAB code? – Cris Luengo Sep 22 '22 at 21:23
  • @CrisLuengo I am working on Java and I am new for JAVA but I know how to program in Matlab so it crosses my mind to use matlab instruction to make my self clear .. Thank you – Dina Sep 29 '22 at 16:21

0 Answers0