librelist archives

« back to archive

Proposal for TNetStrings floats

Proposal for TNetStrings floats

From:
Zed A. Shaw
Date:
2011-05-14 @ 19:19
Apparently, some people really really need floats in TNetStrings.  You
know, for all that incorrectly calculated financial data they want to
send around, or that 3D scene scape they're working on.  Since every
programming language is horrible when it comes to floating point, but
since it's a blocker for some, I propose this for the float spec:

'.' -- Floating point number formatted as "[0-9]*.[0-9]+" format, with
no scientific, IEEE, or commas allowed.  There are *NO* guarantees about
accuracy, efficiency, rounding, or storage given at all.  Just like
"strings" that's left to the application layer.  This format is not
meant to be used to transmit reliable floating point numbers.  If you
want that then use your language to output an IEEE formatted string
blob.

This will make it easy to send and receive in a naive scanf or similar
in any language, and avoids all the endless OCD bikeshedding about
internationalized commas, formats, accuracy etc.

I'll throw it on there, update mongrel2's source and the Python sample,
then see what people think.


-- 
Zed A. Shaw
http://zedshaw.com/

Re: [mongrel2] Proposal for TNetStrings floats

From:
Armando Singer
Date:
2011-05-16 @ 03:25
On May 14, 2011, at 12:19 PM, Zed A. Shaw wrote:

> '.' -- Floating point number formatted as "[0-9]*.[0-9]+" format, with
> no scientific, IEEE, or commas allowed. 

These are signed, right? Should that be:

float:   "(-|+)?[0-9]*\.[0-9]+"
integer: "(-|+)?[0-9]+"

Or maybe just optional leading "-":

float:   "-?[0-9]*\.[0-9]+"
integer: "-?[0-9]+"

Are leading 0's allowed? Or is it:

float:   "-?[1-9]+[0-9]*\.[0-9]+"
integer: "-?[1-9]+[0-9]*"

Cheers,
Armando

Re: [mongrel2] Proposal for TNetStrings floats

From:
Zed A. Shaw
Date:
2011-05-16 @ 17:59
On Sun, May 15, 2011 at 08:25:32PM -0700, Armando Singer wrote:
> On May 14, 2011, at 12:19 PM, Zed A. Shaw wrote:
> 
> > '.' -- Floating point number formatted as "[0-9]*.[0-9]+" format, with
> > no scientific, IEEE, or commas allowed. 
> 
> These are signed, right? Should that be:
> 
> float:   "(-|+)?[0-9]*\.[0-9]+"
> integer: "(-|+)?[0-9]+"

Oh yeah, plus the sign. So this one (remember it's whatever C %f prints
out).

> Are leading 0's allowed? Or is it:
> 
> float:   "-?[1-9]+[0-9]*\.[0-9]+"
> integer: "-?[1-9]+[0-9]*"

No leading zeroes except for the float I think, so:

float:   "(-|+)?[0-9]+\.[0-9]+"
integer: "(-|+)?[1-9][0-9]*"

I think that's right?  So -00000 is invalid, but -0000.10 would be and
if the receiver can't parse it with their float parsing then it's an
error.

That's another thing we can probably add:

"For all formats, the receiver can use whatever built-in type parser
they want and reject anything they don't like, the above regex are only
a *minimum* format they have to accept."

-- 
Zed A. Shaw
http://zedshaw.com/

Re: [mongrel2] Proposal for TNetStrings floats

From:
Armando Singer
Date:
2011-05-16 @ 23:40
On May 16, 2011, at 10:59 AM, Zed A. Shaw wrote:
> No leading zeroes except for the float I think, so:
> 
> float:   "(-|+)?[0-9]+\.[0-9]+"
> integer: "(-|+)?[1-9][0-9]*"
> 
> I think that's right?  So -00000 is invalid, but -0000.10 would be and
> if the receiver can't parse it with their float parsing then it's an
> error.

That looks good to me.

> That's another thing we can probably add:
> 
> "For all formats, the receiver can use whatever built-in type parser
> they want and reject anything they don't like, the above regex are only
> a *minimum* format they have to accept."

That makes sense.

Cheers,
Armando

Re: [mongrel2] Proposal for TNetStrings floats

From:
Ryan Kelly
Date:
2011-05-15 @ 02:19
On Sat, 2011-05-14 at 12:19 -0700, Zed A. Shaw wrote:
>
> '.' -- Floating point number formatted as "[0-9]*.[0-9]+" format, with
> no scientific, IEEE, or commas allowed.  There are *NO* guarantees about
> accuracy, efficiency, rounding, or storage given at all.  Just like
> "strings" that's left to the application layer.  This format is not
> meant to be used to transmit reliable floating point numbers.  If you
> want that then use your language to output an IEEE formatted string
> blob.

How wedded are you to using "." as the type tag?  In the latest release
of my tnetstring module for python I included experimental float support
using "^" as the type tag, would be nice not to have to change it if I
don't need to.


  Cheers,

     Ryan

-- 
Ryan Kelly
http://www.rfk.id.au  |  This message is digitally signed. Please visit
ryan@rfk.id.au        |  http://www.rfk.id.au/ramblings/gpg/ for details

Re: [mongrel2] Proposal for TNetStrings floats

From:
Zed A. Shaw
Date:
2011-05-15 @ 05:16
On Sun, May 15, 2011 at 12:19:48PM +1000, Ryan Kelly wrote:
> On Sat, 2011-05-14 at 12:19 -0700, Zed A. Shaw wrote:
> How wedded are you to using "." as the type tag?  In the latest release
> of my tnetstring module for python I included experimental float support
> using "^" as the type tag, would be nice not to have to change it if I
> don't need to.

That works, ^ it is!  It really doesn't matter much.

-- 
Zed A. Shaw
http://zedshaw.com/

Re: [mongrel2] Proposal for TNetStrings floats

From:
Nicolas Martyanoff
Date:
2011-05-14 @ 19:46
"Zed A. Shaw" <zedshaw@zedshaw.com> writes:

> '.' -- Floating point number formatted as "[0-9]*.[0-9]+" format, with
> no scientific, IEEE, or commas allowed.  There are *NO* guarantees about
> accuracy, efficiency, rounding, or storage given at all.  Just like
> "strings" that's left to the application layer.  This format is not
> meant to be used to transmit reliable floating point numbers.  If you
> want that then use your language to output an IEEE formatted string
> blob.

Hi,

Is there something wrong with floats as byte arrays encoded
as specified by IEEE 754 ?

Regards,

-- 
Nicolas Martyanoff
   http://codemore.org
   khaelin@gmail.com

Re: [mongrel2] Proposal for TNetStrings floats

From:
Zed A. Shaw
Date:
2011-05-15 @ 01:47
On Sat, May 14, 2011 at 09:46:31PM +0200, Nicolas Martyanoff wrote:
> "Zed A. Shaw" <zedshaw@zedshaw.com> writes:
> 
> > '.' -- Floating point number formatted as "[0-9]*.[0-9]+" format, with
> > no scientific, IEEE, or commas allowed.  There are *NO* guarantees about
> > accuracy, efficiency, rounding, or storage given at all.  Just like
> > "strings" that's left to the application layer.  This format is not
> > meant to be used to transmit reliable floating point numbers.  If you
> > want that then use your language to output an IEEE formatted string
> > blob.
> 
> Hi,
> 
> Is there something wrong with floats as byte arrays encoded
> as specified by IEEE 754 ?

Yes, it requires special pack/unpack libraries in most languages, many
don't even know it, and other platforms don't get it.  VS. 123.45 it's
much harder to just load.

-- 
Zed A. Shaw
http://zedshaw.com/

Re: [mongrel2] Proposal for TNetStrings floats

From:
Nicolas Martyanoff
Date:
2011-05-15 @ 08:29
"Zed A. Shaw" <zedshaw@zedshaw.com> writes:

> On Sat, May 14, 2011 at 09:46:31PM +0200, Nicolas Martyanoff wrote:
>> Is there something wrong with floats as byte arrays encoded
>> as specified by IEEE 754 ?
>
> Yes, it requires special pack/unpack libraries in most languages, many
> don't even know it, and other platforms don't get it.  VS. 123.45 it's
> much harder to just load.

It's indeed harder to read (even if it's easy in C, Common Lisp, perl
and ruby, don't know for the others), but it also takes far less space.

Regards,

-- 
Nicolas Martyanoff
   http://codemore.org
   khaelin@gmail.com

Re: [mongrel2] Proposal for TNetStrings floats

From:
Bobby Powers
Date:
2011-05-15 @ 04:13
On Sat, May 14, 2011 at 6:47 PM, Zed A. Shaw <zedshaw@zedshaw.com> wrote:
> On Sat, May 14, 2011 at 09:46:31PM +0200, Nicolas Martyanoff wrote:
>> "Zed A. Shaw" <zedshaw@zedshaw.com> writes:
>>
>> > '.' -- Floating point number formatted as "[0-9]*.[0-9]+" format, with
>> > no scientific, IEEE, or commas allowed.  There are *NO* guarantees about
>> > accuracy, efficiency, rounding, or storage given at all.  Just like
>> > "strings" that's left to the application layer.  This format is not
>> > meant to be used to transmit reliable floating point numbers.  If you
>> > want that then use your language to output an IEEE formatted string
>> > blob.
>>
>> Hi,
>>
>> Is there something wrong with floats as byte arrays encoded
>> as specified by IEEE 754 ?
>
> Yes, it requires special pack/unpack libraries in most languages, many
> don't even know it, and other platforms don't get it.  VS. 123.45 it's
> much harder to just load.

C, Python, and Java (didn't check anywhere else) all support the '%a'
printf format, which I believe allows exact printing and reading in of
double precision data without the need for the crazy algorithms python
uses to print out accurate decimal representations (at a slight loss
of human readability).  fprintf(3) man page says %a is specified in
C99, but isn't in SUSv2.

yours,
Bobby
> --
> Zed A. Shaw
> http://zedshaw.com/
>