Thread: Matlab to C Code Function for Roundup

  1. #1
    Registered User
    Join Date
    Sep 2019
    Posts
    5

    Matlab to C Code Function for Roundup

    Hi, I am trying to create a function for C Code based on my Matlab function for rounding up a value to a user-defined variable.

    I understand that you can use the Ceil function to round up when using printf, but as I am not displaying a lot of my calculations in the console I am looking for another method.

    Below is an example of what I am trying to acheive:

    Any help would be greatly appreciated!

    % Function to round up a number to the specified decimal place
    function return_value = ROUNDUP(value, decimalPlaces)
    % Round the incoming value
    count = 0;
    magnitude = 1;
    for count = 0 : decimalPlaces
    magnitude = magnitude * 10;
    end
    return_value = ceil((value * magnitude) / magnitude);

  2. #2
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    338
    <math.h> has a function called round.

    C - round() function - fresh2refresh.com
    "without goto we would be wtf'd"

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    1,631
    As usual, "Structure" doesn't even bother reading the question.
    Code:
    double roundUpDecimalPlaces(double x, int decimalPlaces) {
        double mag = 1.0;
        for (int i = 0; i < decimalPlaces; ++i)
            mag *= 10;
        return ceil(x * mag) / mag;
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

  4. #4
    Registered User
    Join Date
    Sep 2019
    Posts
    5
    Quote Originally Posted by john.c View Post
    As usual, "Structure" doesn't even bother reading the question.
    Code:
    double roundUpDecimalPlaces(double x, int decimalPlaces) {
        double mag = 1.0;
        for (int i = 0; i < decimalPlaces; ++i)
            mag *= 10;
        return ceil(x * mag) / mag;
    }

    Thanks John. Im new to c code and curious how How does double x pick up the value and should int decimalPlaces be float decimalPlaces?

    Thanks!

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    How does double x pick up the value
    You call the function with the initial value as that argument.

    should int decimalPlaces be float decimalPlaces?
    Of course not, unless you say, have some notion of half a decimal place or 0.34 of a decimal place.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Well... a better approach:
    Code:
    /* Changed 'decimalPlaces' to 'unsigned int' to avoid 'negative'
        values. Notice this value can still be very big, which will cause
        problems! Still. This argument is a count of decimal places, so
        it doesn't make any sense using a floating point type... */
    double roundUpDecimalPlaces(double x, unsigned int decimalPlaces) 
    {
      double mag;
    
      /* Notice: 'decimalPlaces' will be converted to double when calling pow(). */
      mag = pow(10.0, decimalPlaces);    
      return ceil(x * mag) / mag;
    }
    But, of course, keep in mind that floating point is accurate, but not exact in most cases.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error from Matlab to C (just one line code)
    By Elvio Esposito in forum C++ Programming
    Replies: 7
    Last Post: 07-02-2013, 05:55 AM
  2. Replies: 4
    Last Post: 04-14-2012, 03:52 AM
  3. Matlab into C Function
    By a.mlw.walker in forum C Programming
    Replies: 6
    Last Post: 08-12-2011, 01:43 PM
  4. Running C++ Code from Matlab GUI
    By cpp_novice in forum C++ Programming
    Replies: 1
    Last Post: 06-16-2011, 10:42 AM
  5. glue C and matlab code
    By shaoshao in forum C Programming
    Replies: 1
    Last Post: 06-10-2011, 09:13 AM

Tags for this Thread