Safe addition: Difference between revisions

Content added Content deleted
m (syntax highlighting fixup automation)
m (→‎{{header|Wren}}: Minor tidy)
Line 997: Line 997:


However, if Wren is embedded in a suitable C program, then we can ask the latter to call it for us and pass back the result.
However, if Wren is embedded in a suitable C program, then we can ask the latter to call it for us and pass back the result.
<syntaxhighlight lang="ecmascript">/* safe_addition.wren */
<syntaxhighlight lang="wren">/* Safe_addition.wren */
class Interval {
class Interval {
construct new(lower, upper) {
construct new(lower, upper) {
Line 1,026: Line 1,026:


Note that Wren's built-in print statement never displays numbers with more than 14 digit accuracy. However, if the interval bounds had been displayed directly from C with 17 digit accuracy (the maximum for the 'double' type), there would have been a marginal difference between them, namely: [1.2299999999999998, 1.2300000000000002].
Note that Wren's built-in print statement never displays numbers with more than 14 digit accuracy. However, if the interval bounds had been displayed directly from C with 17 digit accuracy (the maximum for the 'double' type), there would have been a marginal difference between them, namely: [1.2299999999999998, 1.2300000000000002].
<syntaxhighlight lang="c">#include <stdlib.h>
<syntaxhighlight lang="c">/* gcc Safe_addition.c -o Safe_addition -lwren -lm */

#include <stdlib.h>
#include <stdio.h>
#include <stdio.h>
#include <math.h>
#include <math.h>
Line 1,092: Line 1,094:
WrenVM* vm = wrenNewVM(&config);
WrenVM* vm = wrenNewVM(&config);
const char* module = "main";
const char* module = "main";
const char* fileName = "safe_addition.wren";
const char* fileName = "Safe_addition.wren";
char *script = readFile(fileName);
char *script = readFile(fileName);
WrenInterpretResult result = wrenInterpret(vm, module, script);
WrenInterpretResult result = wrenInterpret(vm, module, script);