View previous topic :: View next topic |
Author |
Message |
Marcofras n00b

Joined: 29 May 2023 Posts: 52
|
Posted: Tue Apr 22, 2025 12:36 pm Post subject: float not precised |
|
|
Code: |
#include <cstdio>
#include <iostream>
#include <fstream>
#include "colr.hpp"
using namespace std;
int main() {
float di = 200 / 31;
cout << di << endl;
return 0;
}
|
the correct result is 6.4516129, in another program i use the result is 3. |
|
Back to top |
|
 |
grknight Retired Dev

Joined: 20 Feb 2015 Posts: 2120
|
Posted: Tue Apr 22, 2025 12:59 pm Post subject: |
|
|
Try with Code: | float di = 200.f / 31; |
Integer division drops the decimal. Including one float should resolve your issue |
|
Back to top |
|
 |
Marcofras n00b

Joined: 29 May 2023 Posts: 52
|
Posted: Tue Apr 22, 2025 1:06 pm Post subject: |
|
|
now its 6.45161 that is not precised but i can be satisfied.
now i have problem with
Code: |
if (g == 0) {r1 = 0;} else {r1 = (g * 100) / r;}
if (b == 0) {r2 = 0;} else {r2 = (b * 100) / g;}
|
how use the member .f in this expression? |
|
Back to top |
|
 |
logrusx Advocate


Joined: 22 Feb 2018 Posts: 2943
|
Posted: Tue Apr 22, 2025 1:08 pm Post subject: Re: float not precised |
|
|
Marcofras wrote: | Code: |
#include <cstdio>
#include <iostream>
#include <fstream>
#include "colr.hpp"
using namespace std;
int main() {
float di = 200 / 31;
cout << di << endl;
return 0;
}
|
the correct result is 6.4516129, in another program i use the result is 3. |
That's because 200/31 is an expression with higher precedence than assignment and thus gets evaluated a integer division. Then the result is assigned to di.
If you're going to write in C you need to study the order of precedence of operations. In all languages but more so in those with weak types system.
Best Regards,
Georgi |
|
Back to top |
|
 |
grknight Retired Dev

Joined: 20 Feb 2015 Posts: 2120
|
Posted: Tue Apr 22, 2025 1:13 pm Post subject: |
|
|
Marcofras wrote: | now its 6.45161 that is not precised but i can be satisfied.
now i have problem with
Code: |
if (g == 0) {r1 = 0;} else {r1 = (g * 100) / r;}
if (b == 0) {r2 = 0;} else {r2 = (b * 100) / g;}
|
how use the member .f in this expression? |
What are g and r defined as?
"200.f" is a literal expression not a member reference. "200." make a literal 200 into floating-point but defaults to double. Adding "f" after that makes the literal a float. |
|
Back to top |
|
 |
Marcofras n00b

Joined: 29 May 2023 Posts: 52
|
Posted: Tue Apr 22, 2025 1:23 pm Post subject: |
|
|
g and r are unsigned int |
|
Back to top |
|
 |
GDH-gentoo Veteran


Joined: 20 Jul 2019 Posts: 1860 Location: South America
|
Posted: Tue Apr 22, 2025 1:57 pm Post subject: |
|
|
Marcofras wrote: | g and r are unsigned int |
Presumably you mean all of b, g, r1 and r2 are unsigned int, right? And the problem with the two if statements is...?
(Not commenting on the original post since apparently doesn't describe the actual problem) _________________
NeddySeagoon wrote: | I'm not a witch, I'm a retired electronics engineer  |
Ionen wrote: | As a packager I just don't want things to get messier with weird build systems and multiple toolchains requirements though  |
Last edited by GDH-gentoo on Tue Apr 22, 2025 2:00 pm; edited 1 time in total |
|
Back to top |
|
 |
Marcofras n00b

Joined: 29 May 2023 Posts: 52
|
Posted: Tue Apr 22, 2025 2:00 pm Post subject: |
|
|
solved there wasnt problem i was bad data reading.
thanks and bye |
|
Back to top |
|
 |
|