Q16.16 fixed point numbers in go

450 views
Skip to first unread message

Vasiliy Tolstov

unread,
Oct 22, 2018, 8:35:25 AM10/22/18
to golang-nuts
Hi! I have some binary format that contains in 4 bytes Q16.16 fixed
point number in LittleEndian. Does reading this 4 bytes to uint32 and
use math.Float32frombits looks right? And also as i understand if in
my code i use float32 i have performance decrease vs if i work with
fixed point int?


--
Vasiliy Tolstov,
e-mail: v.to...@selfip.ru

Jan Mercl

unread,
Oct 22, 2018, 9:02:41 AM10/22/18
to Vasiliy Tolstov, golang-nuts
On Mon, Oct 22, 2018 at 2:35 PM Vasiliy Tolstov <v.to...@selfip.ru> wrote:

> Does reading this 4 bytes to uint32 and use math.Float32frombits looks right?

--

-j

Vasiliy Tolstov

unread,
Oct 22, 2018, 9:14:44 AM10/22/18
to 0xj...@gmail.com, Василий Толстов, golan...@googlegroups.com
пн, 22 окт. 2018 г. в 16:02, Jan Mercl <0xj...@gmail.com>:
>
> On Mon, Oct 22, 2018 at 2:35 PM Vasiliy Tolstov <v.to...@selfip.ru> wrote:
>
> > Does reading this 4 bytes to uint32 and use math.Float32frombits looks right?
>
> It does not look right, see https://en.wikipedia.org/wiki/Q_(number_format)#Q_to_float


Thanks, so i need to read uint16 Num and uint16 Exp?
After that use number Num*2−exp ?

Vasiliy Tolstov

unread,
Oct 22, 2018, 9:15:34 AM10/22/18
to Василий Толстов, 0xj...@gmail.com, golan...@googlegroups.com
Does it possible to avoid conversation and works only with fixed point
(if this can bring performance speedup)?
пн, 22 окт. 2018 г. в 16:13, Vasiliy Tolstov <v.to...@selfip.ru>:

Ingo Oeser

unread,
Oct 22, 2018, 10:13:52 AM10/22/18
to golang-nuts
https://godoc.org/golang.org/x/image/math/fixed might provide some hints how to work with this kind of type.

The idea is to use a special type and provide all operations you need.

https://godoc.org/math/big#Rat might be helpful to create first tests of the basic math operations with maximum precision, before reducing precision with a conversion to a more limited data type.

Vasiliy Tolstov

unread,
Oct 22, 2018, 6:24:37 PM10/22/18
to night...@googlemail.com, golan...@googlegroups.com
Thanks for suggestions.
If somebody can look at github.com/unistack-org/go-crush package
(ported ceph crush to go) i'l be happy.
Now weight represented as float32, i'm try to use
https://github.com/shopspring/decimal but code looks ugly with many
conversations. And also as i see ceph code (mapper.c for crush) they
does not use something
different for weights but uint32. may be i miss something but i don't
understand how they deal with weights like 0.580 in such cases.
So if somebody wants to help - i'l be happy.
пн, 22 окт. 2018 г. в 17:14, 'Ingo Oeser' via golang-nuts
<golan...@googlegroups.com>:
>
> https://godoc.org/golang.org/x/image/math/fixed might provide some hints how to work with this kind of type.
>
> The idea is to use a special type and provide all operations you need.
>
> https://godoc.org/math/big#Rat might be helpful to create first tests of the basic math operations with maximum precision, before reducing precision with a conversion to a more limited data type.
>
> --
> You received this message because you are subscribed to the Google Groups "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Michael Jones

unread,
Oct 24, 2018, 12:52:27 PM10/24/18
to v.to...@selfip.ru, Ingo Oeser, golan...@googlegroups.com

you can use fixed point all the way...just shift after multiplies and before divides and use double-width (int32) or wider math.
Michael T. Jones
michae...@gmail.com

Scott Cotton

unread,
Oct 25, 2018, 12:57:04 PM10/25/18
to golang-nuts


On Wednesday, 24 October 2018 18:52:27 UTC+2, Michael Jones wrote:

you can use fixed point all the way...just shift after multiplies and before divides and use double-width (int32) or wider math.


Yes, that's close enough for many purposes.  But it gets more complicated with carries/rounding and other functions (such as trig).

Scott 

Michael Jones

unread,
Oct 25, 2018, 4:25:00 PM10/25/18
to w...@iri-labs.com, golan...@googlegroups.com
Guilty. ;-)

It's also more complicated because of the desire for saturating arithmetic.

Vasiliy Tolstov

unread,
Oct 25, 2018, 5:40:09 PM10/25/18
to golan...@googlegroups.com, night...@googlemail.com, michae...@gmail.com
ср, 24 окт. 2018 г. в 19:52, Michael Jones <michae...@gmail.com>:
>
> https://en.wikipedia.org/wiki/Q_(number_format) says it all.
>
> you can use fixed point all the way...just shift after multiplies and before divides and use double-width (int32) or wider math.
>

Thanks!
Reply all
Reply to author
Forward
0 new messages