Calculating the value of e: Difference between revisions
Content added Content deleted
(Calculating the value of e in Chipmunk Basic, GW-BASIC and MSX Basic) |
|||
Line 2,325: | Line 2,325: | ||
=={{header|Java}}== |
=={{header|Java}}== |
||
<p> |
|||
This is can be in Java using the the <code>BigDecimal</code> and <code>BigInteger</code> classes.<br /> |
|||
Each which offer the ability to compute values beyond the constraints of both <code>double</code> and <code>long</code>, respectively. |
|||
</p> |
|||
<p> |
|||
I used a <kbd>rounding-mode</kbd> of "half-up", which rounds 0.5 to 1, and -0.5 to -1. |
|||
</p> |
|||
<p> |
|||
You can set the amount of terms with the <kbd>epsilon</kbd> argument. |
|||
And you can set the scale with the <kbd>scale</kbd> argument, which will ultimately be the <code>int</code> maximum, which is 2,147,483,647. |
|||
</p> |
|||
<syntaxhighlight lang="java"> |
|||
import static java.math.RoundingMode.HALF_UP; |
|||
import java.math.BigDecimal; |
|||
import java.math.BigInteger; |
|||
</syntaxhighlight> |
|||
<syntaxhighlight lang="java"> |
|||
BigDecimal e(BigInteger epsilon, int scale) { |
|||
BigDecimal e = BigDecimal.ONE.setScale(scale, HALF_UP); |
|||
BigDecimal n; |
|||
BigInteger index = BigInteger.ONE; |
|||
while (index.compareTo(epsilon) < 0) { |
|||
n = new BigDecimal(String.valueOf(factorial(index))); |
|||
e = e.add(BigDecimal.ONE.divide(n, scale, HALF_UP)); |
|||
index = index.add(BigInteger.ONE); |
|||
} |
|||
return e; |
|||
} |
|||
BigInteger factorial(BigInteger value) { |
|||
if (value.compareTo(BigInteger.ONE) > 0) { |
|||
return value.multiply(factorial(value.subtract(BigInteger.ONE))); |
|||
} else { |
|||
return BigInteger.ONE; |
|||
} |
|||
} |
|||
</syntaxhighlight> |
|||
<p> |
|||
Here is a execution using 100 terms, and a scale of 1000. |
|||
</p> |
|||
<syntaxhighlight lang="java"> |
|||
BigDecimal e = e(BigInteger.valueOf(100), 1000); |
|||
</syntaxhighlight> |
|||
<pre> |
|||
2.718281828459045235360287471352662497757247093699959574966967627724076630353547 |
|||
59457138217852516642742746639193200305992181741359662904357290033429526059563072 |
|||
73100853237805275106368648701695314186552748459082449550453392864976427741366416 |
|||
59646366325087360915841343970999831703538233800921168146554153749305420222461709 |
|||
32123094916776349931113070302925698934206764391913665038487357884661077572557630 |
|||
79218988673537904194120433774064949070738630790492489764370698362973668621984292 |
|||
50767700214157406500293826954406871877954270969766247465243666295138572019208303 |
|||
17726923409770165674539225777914734160368493572310330448576142902663326352937973 |
|||
44504000613119416470868982597552087347829370853870094341780806567997280704595039 |
|||
17013351431243873005220184059659629058572148124084211850064775039817941961218573 |
|||
36935973323362272606025181783889270251361949206078243869370233748144842017157072 |
|||
21499854656151809995508987059685112005970217969141325866928660231731022979729068 |
|||
783220835224413915990618593145821470347881 |
|||
</pre> |
|||
<br /> |
|||
Anternately, you could use the following implementation. |
|||
{{trans|Kotlin}} |
{{trans|Kotlin}} |
||
<syntaxhighlight lang="java">public class CalculateE { |
<syntaxhighlight lang="java">public class CalculateE { |
||
Line 2,344: | Line 2,404: | ||
{{out}} |
{{out}} |
||
<pre>e = 2.718281828459046</pre> |
<pre>e = 2.718281828459046</pre> |
||
=={{header|Javascript}}== |
=={{header|Javascript}}== |
||
Summing over a scan |
Summing over a scan |