Thread: Understanding the Code

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    35

    Understanding the Code

    I saw the code
    Code:
    typedef signed int SFRAC16;
    #define Q15(Float_Value)    \
            ((Float_Value < 0.0) ? (SFRAC16)(32768 * (Float_Value) - 0.5) \
            : (SFRAC16)(32767 * (Float_Value) + 0.5))
    What is the significance of 0.5? Does 0.5 signify any midpoint?

  2. #2
    Registered User
    Join Date
    May 2019
    Posts
    214
    This code is a slightly questionable attempt at rounding into a 16 bit signed output. I say questionable because depending on "Float_Value", this could overflow.

    The 0.5 is a means of 5/4 rounding of the result into an integer.

    This also makes the assumption, it appears, that at singed int is 16 bits, but that's not guaranteed.
    Last edited by Niccolo; 10-02-2019 at 11:32 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding this code
    By sameertelkar in forum C Programming
    Replies: 11
    Last Post: 01-04-2013, 07:36 AM
  2. Need Help in Understanding this Code
    By Linked_List in forum C Programming
    Replies: 3
    Last Post: 09-04-2012, 09:32 PM
  3. Need Help Understanding This Code.
    By thisbeme in forum C Programming
    Replies: 11
    Last Post: 03-04-2011, 12:01 PM
  4. I need help understanding this code
    By Sshakey6791 in forum C++ Programming
    Replies: 5
    Last Post: 12-23-2008, 02:49 PM
  5. Code Understanding!
    By Jill n Jall in forum C++ Programming
    Replies: 1
    Last Post: 01-16-2003, 04:16 PM

Tags for this Thread