From 10483f9e6491c8b96167a794115bb4829742f9a6 Mon Sep 17 00:00:00 2001 From: Andy Wingo Date: Sun, 8 May 2011 16:05:27 +0200 Subject: [PATCH] fix `hash' for inf and nan * libguile/hash.c (scm_hasher): Fix to work on inf and nan. * test-suite/tests/hash.test ("hash"): Add tests. --- libguile/hash.c | 3 ++- test-suite/tests/hash.test | 7 +++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/libguile/hash.c b/libguile/hash.c index 0dcd1c29e..8448c7ce9 100644 --- a/libguile/hash.c +++ b/libguile/hash.c @@ -26,6 +26,7 @@ #include #endif +#include #include #include "libguile/_scm.h" @@ -192,7 +193,7 @@ scm_hasher(SCM obj, unsigned long n, size_t d) case scm_tc16_real: { double r = SCM_REAL_VALUE (obj); - if (floor (r) == r) + if (floor (r) == r && !isinf (r) && !isnan (r)) { obj = scm_inexact_to_exact (obj); return scm_to_ulong (scm_modulo (obj, scm_from_ulong (n))); diff --git a/test-suite/tests/hash.test b/test-suite/tests/hash.test index d2bde481c..f3d603db3 100644 --- a/test-suite/tests/hash.test +++ b/test-suite/tests/hash.test @@ -1,6 +1,6 @@ ;;;; hash.test --- test guile hashing -*- scheme -*- ;;;; -;;;; Copyright (C) 2004, 2005, 2006, 2008 Free Software Foundation, Inc. +;;;; Copyright (C) 2004, 2005, 2006, 2008, 2011 Free Software Foundation, Inc. ;;;; ;;;; This library is free software; you can redistribute it and/or ;;;; modify it under the terms of the GNU Lesser General Public @@ -32,7 +32,10 @@ (hash #t 0)) (pass-if (= 0 (hash #t 1))) (pass-if (= 0 (hash #f 1))) - (pass-if (= 0 (hash noop 1)))) + (pass-if (= 0 (hash noop 1))) + (pass-if (= 0 (hash +inf.0 1))) + (pass-if (= 0 (hash -inf.0 1))) + (pass-if (= 0 (hash +nan.0 1)))) ;;; ;;; hashv