From cd210c95a5a429293aa5c88965d3526116ba8723 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Tue, 8 Mar 2011 17:49:23 +0000 Subject: The new float format is in place. The encoder/decoder may not be as fast right now as it could be, but it is universal, which is preferable in many cases. We effectively use a normalized base-256 format to store the number, with a scale, also with a base of 256. Basically, with x86 doubles, the C99 standard textual, lossless hex encoding format is at max 23 bytes. This encoding is equivelent but at max 11 bytes, including the format specifier ('f'). --- src/unit/basic.unit | 2 +- src/unit/float.unit | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 src/unit/float.unit (limited to 'src/unit') diff --git a/src/unit/basic.unit b/src/unit/basic.unit index 369e095..3ab8dc2 100644 --- a/src/unit/basic.unit +++ b/src/unit/basic.unit @@ -145,7 +145,7 @@ suite Basic { Gats::Dictionary dict; - dict.insert("bool", true ); + dict.insert("bool", new Gats::Boolean(true) ); dict.insert("int", 33403055 ); dict.insert("str", "Hey there" ); dict.write( mb ); diff --git a/src/unit/float.unit b/src/unit/float.unit new file mode 100644 index 0000000..0473ffb --- /dev/null +++ b/src/unit/float.unit @@ -0,0 +1,75 @@ +// vim: syntax=cpp +/* + * Copyright (C) 2007-2010 Xagasoft, All rights reserved. + * + * This file is part of the libbu++ library and is released under the + * terms of the license contained in the file LICENSE. + */ + +#include "gats/dictionary.h" +#include "gats/float.h" +#include "gats/string.h" + +#include "bu/membuf.h" +#include "bu/list.h" +#include "bu/sio.h" + +#include +#include + +using namespace Bu; + +suite Basic +{ + void rw( double dVal ) + { + Bu::MemBuf mb; + + Gats::Float( dVal ).write( mb ); + + mb.setPos( 0 ); + + Gats::Object *pObj = Gats::Object::read( mb ); + unitTest( pObj != NULL ); + unitTest( pObj->getType() == Gats::typeFloat ); + Gats::Float *pFlt = dynamic_cast(pObj); +// printf("In: %a\nOut: %a\n", dVal, pFlt->getValue() ); + if( isnan( dVal ) ) + unitTest( isnan(pFlt->getValue()) == isnan(dVal) ); + else + unitTest( pFlt->getValue() == dVal ); + unitTest( signbit(pFlt->getValue()) == signbit(dVal) ); + + delete pObj; + } + + test positive + { + rw( 8485738457.0 ); + rw( 0.000000000000001928173 ); + rw( 1.0 ); + rw( 0.0 ); + rw( M_PI ); + } + + test negitave + { + rw( -8485738457.0 ); + rw( -0.000000000000001928173 ); + rw( -1.0 ); + rw( -0.0 ); + rw( -M_PI ); + } + + test inf + { + rw( INFINITY ); + rw( -INFINITY ); + } + + test nan + { + rw( NAN ); + rw( -NAN ); + } +} -- cgit v1.2.3