This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CCS/EK-TM4C123GXL: C "round" function not rounding in CCS

Part Number: EK-TM4C123GXL

Tool/software: Code Composer Studio

I am trying to round a number in CCS using the C "round" function. This should round a number to the closest whole number. For example, I want round(27.7) to equal 28. However, I realized that "round" in my code is not rounding to the nearest whole number.

How do I fix this?

Attached is a snippet of my code. This is the code I want to round. The capitalized words are macros.

uint16_t highValues = round(DATAFREQ * ARRAYSIZE / (2 * PULSESNUMBER * analyzingFreq));


  • For the source file that contains this call to round, please follow the directions in the article How to Submit a Compiler Test Case.  Also tell me the value of the variable analyzingFreq which leads to a wrong result, and what that wrong result is.

    Thanks and regards,

    -George

    1. I am using compiler version TI v18.12.5.LTS.
    2. Attached is my preprocessed source file and build log.
    3. The value of my macros and variables for my current case are as follows:
    1. DATAFREQ = 1300
    2. ARRAYSIZE = 2048
    3. PULSESNUMBER = 16
    4. analyzingFreq = 3000

    The correct value should be 28, but the value I am getting is 27. "round" should be rounding 27.7 to 28. I tried this in the Code::Blocks IDE and it worked successfully there.

    I should add that this code is for a GUI. analyzingFreq is a global variable able to be changed by the user.

    7455.Build Log.txt
    Fullscreen
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    **** Build of configuration Debug for project XCorrelationGUITest1 ****
    "C:\\ti\\ccs930\\ccs\\utils\\bin\\gmake" -k -j 8 all -O
    Building file: "../tm4c123gh6pm_startup_ccs.c"
    Invoking: ARM Compiler
    "C:/ti/ccs930/ccs/tools/compiler/ti-cgt-arm_18.12.5.LTS/bin/armcl" -mv7M4 --code_state=16 --float_support=FPv4SPD16 -me --include_path="C:/Users/leewa/Documents/XCorrelationGUITest1" --include_path="C:/ti/tivaware_c_series_2_1_4_178" --include_path="C:/ti/ccs930/ccs/tools/compiler/ti-cgt-arm_18.12.5.LTS/include" --define=ccs="ccs" --define=PART_TM4C123GH6PM -g --gcc --preproc_with_comment --preproc_with_compile --diag_warning=225 --diag_wrap=off --display_error_number --abi=eabi "../tm4c123gh6pm_startup_ccs.c"
    Finished building: "../tm4c123gh6pm_startup_ccs.c"
    Building target: "XCorrelationGUITest1.out"
    Invoking: ARM Linker
    "C:/ti/ccs930/ccs/tools/compiler/ti-cgt-arm_18.12.5.LTS/bin/armcl" -mv7M4 --code_state=16 --float_support=FPv4SPD16 -me --define=ccs="ccs" --define=PART_TM4C123GH6PM -g --gcc --preproc_with_comment --preproc_with_compile --diag_warning=225 --diag_wrap=off --display_error_number --abi=eabi -z -m"XCorrelationGUITest1.map" --heap_size=16384 --stack_size=16384 -i"C:/ti/ccs930/ccs/tools/compiler/ti-cgt-arm_18.12.5.LTS/lib" -i"C:/ti/ccs930/ccs/tools/compiler/ti-cgt-arm_18.12.5.LTS/include" --reread_libs --diag_wrap=off --display_error_number --warn_sections --xml_link_info="XCorrelationGUITest1_linkInfo.xml" --rom_model -o "XCorrelationGUITest1.out" "./main.obj" "./tm4c123gh6pm_startup_ccs.obj" "C:/ti/tivaware_c_series_2_1_4_178/driverlib/ccs/Debug/driverlib.lib" "../tm4c123gh6pm.cmd" -llibc.a
    <Linking>
    Finished building target: "XCorrelationGUITest1.out"
    **** Build Finished ****
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

  • what type is analyzingFreq? If it is an integer, you are doing all integer arithmetic and the division will truncate.

  • Ah I see. I changed analyzingFreq to a double and I think it fixed the issue. Thank you.

  • You are lucky that the compiler seems to be using 32 bit arithmetic. 1300 * 2048 is more than 2,000,000 which would overflow a 16 bit int.