-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Trying to use SQ31x32 #18
Comments
Unfortunately this is a limitation that I currently don't have a work around for. Fixed point multiplication results in a type twice as large as the source type, which is then scaled back down to the appropriate type. In the case of Truth be told, when I originally wrote this I wasn't anticipating people wanting to use the 64 bit-backed types because I wasn't aware of the ARM-based Arduino MCUs, I was only aware of the AVR-based ones which can only realistically achieve 32-bit backed, 16-bit backed ant 8-bit backed variations. |
Hi, SQ15x16 x = 320.153; z = x*y; The result is z = 0.06349182 however the real value is 0.06403060, that means the error is 0.008. If you are using 16 bits for the decimal part, the resolution should be 2^-16 = 0.00001525878. Thus, why the error started in the thousandth part? |
The error happens when the floats are first converted as well, not just during the multiplication. 320.153 gets stored as 320.1530151367187500 (320 + 10028/65536) 320.1530151367187500 * 0.0001983642578125 = 0.063506915234029293060302734375 So actually 0.0634918212890625 isn't too far off. Unfortunately you'll always have some amount of error because of the difficulty of expressing decimal fractions in binary. For example, you can't exactly store 0.153. If you know that the integer part will have a limited range then you can improve the precision of the fractional part. Using Even a full 32 bits of fraction would only be able to represent 0.153 as 0.1529999971389770. Ultimately depending on what you're calculating, fixed points might not actually be the best solution. |
As it has been 5 days since the last post, @nunovilhena, |
Hello Pharap, Nuno Vilhena |
It's ok, I wasn't trying to rush you. I have opened a second issue (#23) to remind myself to add a proper explanation. As I have answered all your questions, I will close this issue. |
Hello Pharap,
Firstly, I would like to congratulate you for this work. :)
Maybe you can help me with a issue! I'm trying to use the SQ31x32 fixed point type, however I could not compile my code without errors. If I use the SQ15x16 type, I can compile and run my code but if I change to SQ31x32 I can't compile it.
I'm trying to do a simple multiplication:
SQ31x32 x = 1;
SQ31x32 y = 0.000200;
SQ31x32 z = 0.0;
z= x * y;
Could you help me or give me some feedback?
Thank you so much.
Nuno Vilhena
The text was updated successfully, but these errors were encountered: