math.parser: expose a format-float primitive for use by formatting vocabulary

release
Slava Pestov 2010-04-13 21:21:28 -07:00
parent e1ee2c82ea
commit 288090d993
6 changed files with 16 additions and 13 deletions

View File

@ -289,7 +289,7 @@ M: bad-executable summary
\ (dlsym) { byte-array object } { c-ptr } define-primitive \ (dlsym) { byte-array object } { c-ptr } define-primitive
\ (exists?) { string } { object } define-primitive \ (exists?) { string } { object } define-primitive
\ (exit) { integer } { } define-primitive \ (exit) { integer } { } define-primitive
\ (float>string) { float } { byte-array } define-primitive \ (float>string) make-foldable \ (format-float) { float byte-array } { byte-array } define-primitive \ (format-float) make-foldable
\ (fopen) { byte-array byte-array } { alien } define-primitive \ (fopen) { byte-array byte-array } { alien } define-primitive
\ (identity-hashcode) { object } { fixnum } define-primitive \ (identity-hashcode) { object } { fixnum } define-primitive
\ (save-image) { byte-array byte-array } { } define-primitive \ (save-image) { byte-array byte-array } { } define-primitive

View File

@ -470,7 +470,7 @@ tuple
{ "byte-array>bignum" "math" "primitive_byte_array_to_bignum" (( x -- y )) } { "byte-array>bignum" "math" "primitive_byte_array_to_bignum" (( x -- y )) }
{ "double>bits" "math" "primitive_double_bits" (( x -- n )) } { "double>bits" "math" "primitive_double_bits" (( x -- n )) }
{ "float>bits" "math" "primitive_float_bits" (( x -- n )) } { "float>bits" "math" "primitive_float_bits" (( x -- n )) }
{ "(float>string)" "math.parser.private" "primitive_float_to_str" (( n -- str )) } { "(format-float)" "math.parser.private" "primitive_format_float" (( n format -- byte-array )) }
{ "bignum*" "math.private" "primitive_bignum_multiply" (( x y -- z )) } { "bignum*" "math.private" "primitive_bignum_multiply" (( x y -- z )) }
{ "bignum+" "math.private" "primitive_bignum_add" (( x y -- z )) } { "bignum+" "math.private" "primitive_bignum_add" (( x y -- z )) }
{ "bignum-" "math.private" "primitive_bignum_subtract" (( x y -- z )) } { "bignum-" "math.private" "primitive_bignum_subtract" (( x y -- z )) }

View File

@ -1,6 +1,7 @@
! (c)2009 Joe Groff bsd license ! (c)2009 Joe Groff bsd license
USING: accessors combinators kernel kernel.private math USING: accessors byte-arrays combinators kernel kernel.private
namespaces sequences sequences.private splitting strings make ; math namespaces sequences sequences.private splitting strings
make ;
IN: math.parser IN: math.parser
: digit> ( ch -- n ) : digit> ( ch -- n )
@ -356,15 +357,15 @@ M: ratio >base
mantissa-expt [ float>hex-value ] [ float>hex-expt ] bi* mantissa-expt [ float>hex-value ] [ float>hex-expt ] bi*
] bi 3append ; ] bi 3append ;
: float>decimal ( n -- str ) : format-float ( n format -- string )
(float>string) 0 suffix >byte-array (format-float)
[ 0 = ] trim-tail >string dup [ 0 = ] find drop head >string
fix-float ; fix-float ;
: float>base ( n base -- str ) : float>base ( n base -- str )
{ {
{ 16 [ float>hex ] } { 16 [ float>hex ] }
[ drop float>decimal ] [ drop "%.16g" format-float ]
} case ; inline } case ; inline
PRIVATE> PRIVATE>

View File

@ -260,10 +260,12 @@ void factor_vm::primitive_bignum_to_float()
ctx->replace(allot_float(bignum_to_float(ctx->peek()))); ctx->replace(allot_float(bignum_to_float(ctx->peek())));
} }
void factor_vm::primitive_float_to_str() void factor_vm::primitive_format_float()
{ {
byte_array *array = allot_byte_array(33); byte_array *array = allot_byte_array(100);
SNPRINTF((char *)(array + 1),32,"%.16g",untag_float_check(ctx->pop())); char *format = alien_offset(ctx->pop());
double value = untag_float_check(ctx->pop());
SNPRINTF(array->data<char>(),99,format,value);
ctx->push(tag<byte_array>(array)); ctx->push(tag<byte_array>(array));
} }

View File

@ -82,8 +82,8 @@ namespace factor
_(float_subtract) \ _(float_subtract) \
_(float_to_bignum) \ _(float_to_bignum) \
_(float_to_fixnum) \ _(float_to_fixnum) \
_(float_to_str) \
_(fopen) \ _(fopen) \
_(format_float) \
_(fputc) \ _(fputc) \
_(fread) \ _(fread) \
_(fseek) \ _(fseek) \

View File

@ -464,7 +464,7 @@ struct factor_vm
cell unbox_array_size_slow(); cell unbox_array_size_slow();
void primitive_fixnum_to_float(); void primitive_fixnum_to_float();
void primitive_bignum_to_float(); void primitive_bignum_to_float();
void primitive_float_to_str(); void primitive_format_float();
void primitive_float_eq(); void primitive_float_eq();
void primitive_float_add(); void primitive_float_add();
void primitive_float_subtract(); void primitive_float_subtract();