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

  • SEARCH
  • Home
  • 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 8712171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:03:23+00:00 2026-06-13T05:03:23+00:00

I am trying to do some simple variable manipulation in a embedded C/C++ environment.

  • 0

I am trying to do some simple variable manipulation in a embedded C/C++ environment. For some reason I consistently am getting zeros as a result.

Here is the code

void Heater::setPower( int newpower ) 
{
    printf("1 new power %d\n",newpower);

    if(newpower != power)
        power = newpower;
    else 
        return;

    printf("2 new power %d\n",power);

    // Set Duty
    long unsigned int newduty = 0;

    // Protect from divide by zero
    if(power <= 0)
    {
         printf("2.5 zeroed\n");
         newduty = 0;
         power = 0; 
    }
    else
         newduty = period*(power/100);

    printf("3 setduty %lu period %lu\n", newduty, period);

    setDuty(newduty);
}

Here is the output I receive

1 new power 76
2 new power 76
3 setduty 0 period 10000000

So I know that the received number is 76. I know is makes it past the first hurdle, and the second. But somehow in the simple math equation it becomes zero. “period” is a long unsigned int as well and is declared in the class def, but you can see the output is appropriate.

Why is this dropping to zero consistently? What is missing? Do I need to include something special for the larger numbers or can I not use simple operators * and / on a long unsigned int or something?

I am dealing with high numbers (x<=10,000,000) because I am using kernel level pwm.

  • 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-06-13T05:03:24+00:00Added an answer on June 13, 2026 at 5:03 am

    The problem you’re having is that power is an integer, so when you /100 you’re actually ending up with a result of 0 due to the rules of integer division. This means that when you multiply it by period, the whole result is 0.

    Depending on your precision requirements it may be sufficient to make to make power a double, however be aware that (power/100)*period will generally produce a fractional result and so storing the result in a double may also be required.

    If you have to stick with integers just do ((power*period)/100) this will give you a reasonably accurate result since power*period becomes a large number before the division makes anything a 0. With integer division, where you put the brackets can be the difference between getting a 0 or 7600000 (in your case).

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

Sidebar

Related Questions

I am trying to write some simple code which will read a text file
I'm trying to get some simple piece of code I found on a website
I am trying to write some simple predicate using boost::lambda and I am getting
I'm trying to use some simple AJAX and then passing my variable through a
I'm trying to split some prior crafted code into a DLL. It's a simple
Here is a simple example of some code that compiles using Java 6, but
I'm trying to convert some simple .htaccess rewrites into web.config xml; all is well
I'm trying to do some simple things in C and I'm confused. The program
I was trying to place some simple effects on an image using HBox using
I am trying to create some simple polygons in openGL3.3. I have 2 types

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.