## Fixed Point Math help please

6 posts / 0 new
Author
Message

Hello guys , Today i decided to study the Fixed point math , so here is a code i am working on ... i am assuming that i have 2 variables x =1.2 and z =3.2 but they are scaled by 16 so X=19 and Z=51 ,

I want to Add X+Z and store the result in Result variable which is going to be equal to 70 which is in the form of UQ4_4(UQ4.4) iam working on unsigned Q ...

70 is represented in binary as 0100.0110 ,,, the decimal point is just virtual my problem is how can i get the fractional part?
i did ANDing the 70 with 0x0F to extract the 4 fractional bits which is equal to 6 in decimal but the value should be 0.375 ... so when i divide 6 / 16 I get 0.375 .. the problem is how can i get 0.375 on AVR to display 4.375 which is an approximated result of adding 1.2 +3.2

Last Edited: Mon. Mar 23, 2020 - 07:58 PM

Surely, it is a general programming question - nothing specific to AVR ?

Thus it would be a lot easier to experiment on a PC ...

Top Tips:

1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...

I want to apply this on AVR , i am using something like this in my AVR code

Before you can apply it, you need to understand it.

Gaining the understanding is not specific to AVR - in fact, the limitations & complications of the AVR are likely to impede your learning.

The principles are not specific to AVR - they are the same on any platform!

https://en.wikipedia.org/wiki/Q_(number_format)

Top Tips:

1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Mon. Mar 23, 2020 - 07:06 PM

Please do not post the same question in multiple sub-forums.

I have asked the Moderators to lock the other copy of this.

Thank you,

JC

LOCKED

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

"The critical shortage here is not stuff, but time." - Johan Ekdahl

"Step N is required before you can do step N+1!" - ka7ehk

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown