From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by yquem.inria.fr (Postfix) with ESMTP id 61DDCBBAF for ; Mon, 23 Jun 2008 02:49:19 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AnUBABaQXkjYi40Ilmdsb2JhbACSbAEBAQEJBQgYml8 X-IronPort-AV: E=Sophos;i="4.27,686,1204498800"; d="scan'208";a="14257510" Received: from mail-out8.nyct.net (HELO mail.nyct.net) ([216.139.141.8]) by mail1-smtp-roc.national.inria.fr with ESMTP; 23 Jun 2008 02:49:18 +0200 Received: from [192.168.42.2] (pool-96-250-28-207.nycmny.east.verizon.net [96.250.28.207]) (authenticated bits=0) by mail.nyct.net (8.14.2/8.14.1) with ESMTP id m5N0nFbH056450 (version=TLSv1/SSLv3 cipher=DHE-DSS-AES256-SHA bits=256 verify=NO); Sun, 22 Jun 2008 20:49:17 -0400 (EDT) (envelope-from bhurt@spnz.org) Date: Sun, 22 Jun 2008 21:25:15 -0400 (EDT) From: Brian Hurt X-X-Sender: bhurt@localhost To: Paolo Donadeo Cc: caml-list caml-list Subject: Re: [Caml-list] Strange behaviour of string_of_float In-Reply-To: <4b5157c30806221345p9be5daeqc65b3f6cfb3219b3@mail.gmail.com> Message-ID: References: <4b5157c30806220956p164d8346i3e832bc8b8666306@mail.gmail.com> <20080622195831.GA22384@annexia.org> <4b5157c30806221345p9be5daeqc65b3f6cfb3219b3@mail.gmail.com> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed X-Spam: no; 0.00; serialize:01 deserialize:01 ocaml:01 ocaml:01 printf:01 sprintf:01 wrote:01 encode:01 caml-list:01 int:01 int:01 behaviour:01 exp:02 exp:02 match:02 On Sun, 22 Jun 2008, Paolo Donadeo wrote: > I know what a float number is from my numerical analysis course :-). > > In any case, what is the suggested way to serialize/deserialize a > float number in OCaml? The Sexplib, for example, suffers the same > problem of the string_of_float function:. > > My intent is to extract an ASCII representation of an OCaml float > value so that it can be used to recreate *exactly* the same value, at > least on the same architecture. > Code something like this should work: let encode_float x = match (classify_float x) with | FP_zero -> if (x = -0.0) then "-0.0" else "0.0" | FP_infinite -> if (x = neg_infinity) then "-INF" else "INF" | FP_nan -> "NaN" | _ -> let s = x < 0.0 in let x = abs_float x in let frac, exp = frexp x in let frac = frac *. 268435456.0 in (* 2^28 *) let i1 = int_of_float frac in let i2 = int_of_float ((frac -. (floor frac)) *. 268435456.0) in let exp = exp - 56 in let s2 = exp < 0 in let exp = if exp < 0 then -exp else exp in Printf.sprintf "%c%07X%07XX%c%X" (if s then '-' else '+') i1 i2 (if s2 then '-' else '+') exp ;; I'll leave the decode to you- it should be obvious, once you discover the ldexp function. Brian