Fixed Point Math help please

Go To Last Post
6 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

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
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Isn't this just the same as your other thread  https://www.avrfreaks.net/forum/q-format-number-binary-scaling ?

 

 

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...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

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

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

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
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

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

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

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

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

Topic locked