Count in octal: Difference between revisions

Content added Content deleted
(Go solution)
(→‎{{header|Go}}: added floating point example)
Line 164: Line 164:
177776
177776
177777
177777
</pre>
Note also that if floating point types are used for the counter, loss of precision will prevent the program from from ever reaching the maximum value. If you stretch interpretation of the task wording "maximum value" to mean "maximum value of contiguous integers" then the following will work:
<lang go>import "fmt"

func main() {
for i := 0.; ; {
fmt.Printf("%o\n", int64(i))
/* uncomment to produce example output
if i == 3 {
i = float64(1<<53 - 4) // skip to near the end
fmt.Println("...")
} */
next := i + 1
if next == i {
break
}
i = next
}
}</lang>
Output, with skip uncommented:
<pre>
0
1
2
3
...
377777777777777775
377777777777777776
377777777777777777
400000000000000000
</pre>
</pre>
Big integers have no maximum value, but the Go runtime will panic when memory allocation fails. The deferred recover here allows the program to terminate silently should the program run until this happens.
Big integers have no maximum value, but the Go runtime will panic when memory allocation fails. The deferred recover here allows the program to terminate silently should the program run until this happens.