Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 359155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T12:22:02+00:00 2026-05-12T12:22:02+00:00

I have code written in C which is intended for a 16-bit microcontroller. The

  • 0

I have code written in C which is intended for a 16-bit microcontroller. The code essentially does lot of floating point arithmetic.

The arithmetic works fine till the result is positive, but in case of subtraction, if the expected result is negative, I get a zero.

result = 0.005 - 0.001;      Is correctly computed as 0.004
result = 0.001 - 0.005;      Is always zero.

Why is there such a behavior for float?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T12:22:02+00:00Added an answer on May 12, 2026 at 12:22 pm

    Interesting. It could quite easily be a fault in the floating point software. Embedded systems often include floating point as an option so as to keep the code size down.

    I’m not sure that’s the problem here though since your first statement works.

    What happens with:

    result = 0.005 - 0.001;
    result = -result;
    
    result = 0.002 - 0.001;
    result = 0.002 - 0.002;
    result = 0.002 - 0.003;
    
    result = 0.001 - 0.002;
    result = 0.001 - 0.003;
    result = 0.001 - 0.004;
    

    The idea here is to collect useful information as to what could be causing it, a common thing to do in forensics. The results of those calculations are likely to be helpful in determining the actual problem.

    Based on your results in the comments:

    result = 0.005 - 0.001;  // 0.004
    result = -result;        // 0.000
    result = 0.002 - 0.001;  // 0.001
    result = 0.002 - 0.002;  // 0.000
    result = 0.002 - 0.003;  // 0.000
    result = 0.001 - 0.002;  // 0.000
    result = 0.001 - 0.003;  // 0.000
    result = 0.001 - 0.004;  // 0.000
    

    It looks like your floating point library has a serious shortcoming. Two more questions:

    • How are you printing out the results (show us the actual code)?
    • Which micro-controller and development environment are you using?

    There may be some problem with the way you’re printing or it may be a limitation of your environment.

    Ajit, I think you’re really going to have to give us some code to help you out. Not necessarily your real code (your concern about releasing the real code is understood), just some that demonstrates the problem.

    Based on some of your comments, to wit:

    Adriaan, the datatype for “result” is of float, that is, 32-bit representation (single). I have CAN as the system interface, and hence I am multiplying the result by 1000 to send it over the CAN bus. If it happens to be a negative number, like -0.003, then I am expecting FF FD in the CAN message. I do not have a debugger.

    I’m not sure I totally understand, but I’ll give it a shot.

    You have a 32-bit float, for example, -0.003 and you multiply it by 1000 and put it in an integer (0xFFFD is the 16-bit two’s complement representation of -3). So what happens when you run something like the following code:

    int main(void) {
        float w = -0.003;
        int x = (int)(w * 1000);
        int y = -3;
        int z = -32768;
        // Show us you code here for printing x, y and z.
        return 0;
    }
    

    The reason I want you to test an integer is that it may have nothing to do with floats at all. It may be that the float value is perfectly correct, but there’s some problem with the way you’re printing it (the CAN method).

    If “CAN” is some sort of serial interface, it may be there’s a restriction on the bytes you’re allowed to send across it. I can envisage a scenario where the high bytes are used as packet markers so that FF may actually end the message prematurely. That’s why I also want you to test -32768 (0x8000).

    It’s hard to believe that STMicroelectronics would produce such a braindead runtime system that it couldn’t handle negative floats. It seems much more likely to me that the information is being corrupted somewhere else (for example, the “printing” process, whatever that may be).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 203k
  • Answers 203k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer What about adding a sheet to the workbook that pulls… May 12, 2026 at 8:32 pm
  • Editorial Team
    Editorial Team added an answer NSInteger insertionPoint = [[[myTextView selectedRanges] objectAtIndex:0] rangeValue].location; May 12, 2026 at 8:32 pm
  • Editorial Team
    Editorial Team added an answer You might try styling it with overflow: visible; You could… May 12, 2026 at 8:32 pm

Related Questions

I have an out-of-process COM server written in C++, which is called by some
I have a loop written in C++ which is executed for each element of
I have backend code written in Java. The code has about 12 classes. However
i have a .net dll which is written in c sharp, which uses linq

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.