|
Revision 3806, 1.2 KB
(checked in by dr2chase, 6 months ago)
|
|
Looking into issues of factorial-of-zz64 raised at hands-on-lab; new demo shows how to get it right
|
| Line | |
|---|
| 1 | (******************************************************************************* |
|---|
| 2 | Copyright 2009 Sun Microsystems, Inc., |
|---|
| 3 | 4150 Network Circle, Santa Clara, California 95054, U.S.A. |
|---|
| 4 | All rights reserved. |
|---|
| 5 | |
|---|
| 6 | U.S. Government Rights - Commercial software. |
|---|
| 7 | Government users are subject to the Sun Microsystems, Inc. standard |
|---|
| 8 | license agreement and applicable provisions of the FAR and its supplements. |
|---|
| 9 | |
|---|
| 10 | Use is subject to license terms. |
|---|
| 11 | |
|---|
| 12 | This distribution may include materials developed by third parties. |
|---|
| 13 | |
|---|
| 14 | Sun, Sun Microsystems, the Sun logo and Java are trademarks or registered |
|---|
| 15 | trademarks of Sun Microsystems, Inc. in the U.S. and other countries. |
|---|
| 16 | ******************************************************************************) |
|---|
| 17 | |
|---|
| 18 | component fact64 |
|---|
| 19 | export Executable |
|---|
| 20 | |
|---|
| 21 | (* Computing factorial, in 64 bits. Notice the call to "widen"; |
|---|
| 22 | this is necessary at least with the current interpreter, which |
|---|
| 23 | otherwise cheerfully multiplies two 32-bit integers giving a 32-bit |
|---|
| 24 | result, in a way that would not surprise Kernighan and Ritchie |
|---|
| 25 | in the least. *) |
|---|
| 26 | |
|---|
| 27 | run() = do |
|---|
| 28 | for i <- seq(0#20) do |
|---|
| 29 | j:ZZ64 = widen(i) |
|---|
| 30 | println("fact(" j ")= " f(j)) |
|---|
| 31 | end |
|---|
| 32 | end |
|---|
| 33 | |
|---|
| 34 | f(x:ZZ64):ZZ64 = if x < 2 then 1 else x f(x-1) end |
|---|
| 35 | end |
|---|