How to work with DEC(16) DEC(34) in C/C++ embedded firebird

35 views
Skip to first unread message

eumir camara

unread,
Aug 21, 2022, 3:29:13 AM8/21/22
to firebird-support
i'm working on GO language and trying to access firebird thru fbclient.dll. This means that I have to use CGO to access fbclient.dll. I am in the midst of retreiving  DEC(16) and DEC(34) data but lack the functions necessary to manipulate DEC(16) & DEC(34) into strings and other data types. I went thru firebird source code and wasn't able to find exported API to allow me to manipulate DECFLOATs DEC(16) & DEC(34).

Can anyone lead me to a solution?

Thanks.

Mark Rotteveel

unread,
Aug 21, 2022, 3:59:52 AM8/21/22
to firebird...@googlegroups.com
A DECFLOAT(16) is a 64-bit IEEE 754 decimal64, and a DECFLOAT(34) is a
128-bit IEEE 754 decimal128. Firebird itself uses the decNumber library
to manipulate them. See http://speleotrove.com/decimal/#decNumber for
this library. Firebird includes this code in extern/decnumber. See also
the code in src/common/DecFloat.cpp which defines the operations
Firebird supports on decfloat (using decNumber).

For example, obtaining a decimal128 from string is done with
decQuadFromString, conversion to string with decQuadToString, addition
is decQuadAdd, etc.

I don't know the Go ecosystem well enough to say if there is a Go
library that supports decimal64 and decimal128 manipulation, but I
believe you should be able to bind that decNumber library in Go.

Alternatively, if you want them as string, you could use the
isc_dpb_set_bind connection property or the SET BIND statement to define
a mapping to string, so Firebird coerces the data types to/from string
when sending to or receiving from the client. This can be done by
setting isc_dpb_set_bind to "decfloat to varchar", or by executing

set bind of decfloat to varchar;

You could also bind to - for example - double precision, but then you
lose precision and range.

Mark
--
Mark Rotteveel

Dimitry Sibiryakov

unread,
Aug 21, 2022, 5:00:58 AM8/21/22
to firebird...@googlegroups.com
eumir camara wrote 21.08.2022 6:16:
> Can anyone lead me to a solution?

IUtil interface has methods to get IDecFloat16/32 interfaces that perform
transformation from/into string and BCD forms.

--
WBR, SD.
Reply all
Reply to author
Forward
0 new messages