Dividing and multiplying numbers leaves wronge results on stm32ide [duplicate]

Recently I have faced a problem that did not exist in my code before. When dividing or multiplying any type of numbers, I get wrong decimals. For example: 30/100 = 29.999999 (both defined float and are poured into a float valuable) or volume = 1500 multiply by 1500 multiply by 778 = 1750499968 (both 1500 are defined int and 778 is defined float eventhough it will not have decimals, volume was tested by being defined uint32_t or float or double, the result is the same) For deviding, I have tried 30.0/100.0 and 30.0f/100.0f too and the problem still exists, and with different type of number such as float a = 30; then a/100.0 or int a = 30; (float)(a)/100.0 I'm programming different STM32 mcus such as STM32L4 and STM32F4 and STM32F1. Also have tried different codes written from the start specifically to test and fix this problem. This has caused problems in my codes since I need to use correct decimals in my calculations and then show the result as int through modbus. My inputs are as int which will be divided by 1000 and 100. I'm also worried this problem exists in my other calculations which brings more errors to results. Is there a way to fix this? I think it's a problem that happened in my CubeIDE program. Because it did not exist before and i dont know what i might have changed for this to happen. My system is Windows 7, 64bit and IDE version 1.13.1 but also 1.17 same problem exists. Unfortunately can not upgrade windows since it is a work system. Thank you! EDIT : regarding this link : What causes floating point rounding errors? as far as i read this link is explaining rounding error and why it exists. I am already aware of that. Can anyone suggest a way to handle this? or to only use 4 decimal points? because even multiplying 1500 by 778 is causinga problem. let alone dividing.

Mar 1, 2025 - 12:40
 0
Dividing and multiplying numbers leaves wronge results on stm32ide [duplicate]

Recently I have faced a problem that did not exist in my code before. When dividing or multiplying any type of numbers, I get wrong decimals. For example: 30/100 = 29.999999 (both defined float and are poured into a float valuable)

or

volume = 1500 multiply by 1500 multiply by 778 = 1750499968 (both 1500 are defined int and 778 is defined float eventhough it will not have decimals, volume was tested by being defined uint32_t or float or double, the result is the same)

For deviding, I have tried 30.0/100.0 and 30.0f/100.0f too and the problem still exists, and with different type of number such as float a = 30;

then a/100.0

or int a = 30;

(float)(a)/100.0

I'm programming different STM32 mcus such as STM32L4 and STM32F4 and STM32F1. Also have tried different codes written from the start specifically to test and fix this problem.

This has caused problems in my codes since I need to use correct decimals in my calculations and then show the result as int through modbus. My inputs are as int which will be divided by 1000 and 100.

I'm also worried this problem exists in my other calculations which brings more errors to results.

Is there a way to fix this? I think it's a problem that happened in my CubeIDE program. Because it did not exist before and i dont know what i might have changed for this to happen. My system is Windows 7, 64bit and IDE version 1.13.1 but also 1.17 same problem exists. Unfortunately can not upgrade windows since it is a work system.

Thank you!

EDIT : regarding this link : What causes floating point rounding errors?

as far as i read this link is explaining rounding error and why it exists. I am already aware of that. Can anyone suggest a way to handle this? or to only use 4 decimal points? because even multiplying 1500 by 778 is causinga problem. let alone dividing.