From 8c94a00e02a0fe610573302096294a9b6aa50e43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20H=C3=B8is=C3=A6ther=20Rasch?= Date: Fri, 27 Oct 2017 11:34:57 +0200 Subject: [PATCH] fixes #6606 (#6610) --- lib/nimbase.h | 7 ++++++- tests/float/tfloatnan.nim | 22 ++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 tests/float/tfloatnan.nim diff --git a/lib/nimbase.h b/lib/nimbase.h index c06c45691..76192713b 100644 --- a/lib/nimbase.h +++ b/lib/nimbase.h @@ -396,8 +396,13 @@ typedef struct TStringDesc* string; #define GenericSeqSize sizeof(TGenericSeq) #define paramCount() cmdCount +// NAN definition copied from math.h included in the Windows SDK version 10.0.14393.0 #ifndef NAN -# define NAN (0.0 / 0.0) +#ifndef _HUGE_ENUF +#define _HUGE_ENUF 1e+300 // _HUGE_ENUF*_HUGE_ENUF must overflow +#endif +#define NAN_INFINITY ((float)(_HUGE_ENUF * _HUGE_ENUF)) +#define NAN ((float)(NAN_INFINITY * 0.0F)) #endif #ifndef INF diff --git a/tests/float/tfloatnan.nim b/tests/float/tfloatnan.nim new file mode 100644 index 000000000..66cb8001d --- /dev/null +++ b/tests/float/tfloatnan.nim @@ -0,0 +1,22 @@ +discard """ + file: "tfloatnan.nim" + output: '''Nim: nan +Nim: nan (float) +C: nan (float) +Nim: nan (double) +C: nan (double) +''' +""" + +proc printf(formatstr: cstring): int {.importc: "printf", varargs, header: "".} + +let f = NaN +echo "Nim: ", f + +let f32: float32 = NaN +echo "Nim: ", f32, " (float)" +discard printf("C: %f (float)\n", f32) + +let f64: float64 = NaN +echo "Nim: ", f64, " (double)" +discard printf("C: %lf (double)\n", f64)