From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id JAA14525 for caml-redistribution; Mon, 14 Sep 1998 09:55:20 +0200 (MET DST) Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id JAA18419 for ; Mon, 14 Sep 1998 09:41:26 +0200 (MET DST) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by concorde.inria.fr (8.8.7/8.8.7) with ESMTP id JAA19722; Mon, 14 Sep 1998 09:41:22 +0200 (MET DST) Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id JAA09810; Mon, 14 Sep 1998 09:41:21 +0200 (MET DST) From: Pierre Weis Message-Id: <199809140741.JAA09810@pauillac.inria.fr> Subject: Re: Bug converting numbers? To: mottl@miss.wu-wien.ac.at (Markus Mottl) Date: Mon, 14 Sep 1998 09:41:21 +0200 (MET DST) Cc: caml-list@inria.fr In-Reply-To: <199809121222.OAA29992@miss.wu-wien.ac.at> from "Markus Mottl" at Sep 12, 98 02:22:12 pm X-Mailer: ELM [version 2.4 PL24 ME8] MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit Sender: weis > Hello - Bonjour! > > I have encountered unexpected behaviour converting integers to floats: > > # let x = 7.;; > val x : float = 7 > # let y = float 7;; > val y : float = 7 > # x == y;; > - : bool = false > > The internal representation of "7." is obviously different to "float 7". > Is there some reason to it? This makes it hard to write things like: > > if a_float == float (truncate a_float) ... > > if one wants to check whether a float can actually be represented as > an integer... For a more exhaustive discussion on this problem, see the Caml FAQ: . The problem here is that you missed the right equality predicate: you must use = instead of ==. Remember that == means unicity of representation, not semantical equality of values. For instance: # "ok" == "ok";; - : bool = false "ok" is not the same object as (another) "ok". In contrast, "ok" is equal to (any other) "ok": # "ok" = "ok";; - : bool = true So there is no problem of floating point numbers conversion, since the same (unexpected ?) behaviour still apply if we remove conversions: # 1.0 == 1.0;; - : bool = false (it means that floats may be allocated and then stored in different memory locations). Using = your predicate works fine: let represented_has_an_integer f = f = float (truncate f);; val represented_has_an_integer : float -> bool = And now: # represented_has_an_integer 7.0;; - : bool = true Remember that the == predicate is not for casual uses: you should know something about value representations in your Caml system to use it safely. In doubt, use the regular = predicate. > français: > La représentation de "7." et évidemment differente de "float 7". > Est-ce qu'il y a une raison? C'est un problème si on veut verifier si un > "float" peut être représenté comme "int"... Pour une discussion plus complète du problème voir . Le comportement que vous observez vient de ce que les flottants sont des structures de données (généralement) allouées en mémoire. Or vous utilisez le prédicat == qui teste l'identité physique, au lieu de = qui teste l'égalité sémantique. Le test == donne alors des résultats surprenants: # 1.0 == 1.0;; - : bool = false Il suffit d'utiliser = pour que tout rentre dans l'ordre. Pierre Weis INRIA, Projet Cristal, Pierre.Weis@inria.fr, http://cristal.inria.fr/~weis/