Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
float not precised
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
Marcofras
n00b
n00b


Joined: 29 May 2023
Posts: 52

PostPosted: Tue Apr 22, 2025 12:36 pm    Post subject: float not precised Reply with quote

Code:

#include <cstdio>
#include <iostream>
#include <fstream>
#include "colr.hpp"
using namespace std;

int main() {
                  float di = 200 / 31;
                  cout << di << endl;
                  return 0;
                  }

Code:

6

the correct result is 6.4516129, in another program i use the result is 3.
Back to top
View user's profile Send private message
grknight
Retired Dev
Retired Dev


Joined: 20 Feb 2015
Posts: 2120

PostPosted: Tue Apr 22, 2025 12:59 pm    Post subject: Reply with quote

Try with
Code:
float di = 200.f / 31;

Integer division drops the decimal. Including one float should resolve your issue
Back to top
View user's profile Send private message
Marcofras
n00b
n00b


Joined: 29 May 2023
Posts: 52

PostPosted: Tue Apr 22, 2025 1:06 pm    Post subject: Reply with quote

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
View user's profile Send private message
logrusx
Advocate
Advocate


Joined: 22 Feb 2018
Posts: 2943

PostPosted: Tue Apr 22, 2025 1:08 pm    Post subject: Re: float not precised Reply with quote

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;
                  }

Code:

6

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
View user's profile Send private message
grknight
Retired Dev
Retired Dev


Joined: 20 Feb 2015
Posts: 2120

PostPosted: Tue Apr 22, 2025 1:13 pm    Post subject: Reply with quote

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
View user's profile Send private message
Marcofras
n00b
n00b


Joined: 29 May 2023
Posts: 52

PostPosted: Tue Apr 22, 2025 1:23 pm    Post subject: Reply with quote

g and r are unsigned int
Back to top
View user's profile Send private message
GDH-gentoo
Veteran
Veteran


Joined: 20 Jul 2019
Posts: 1860
Location: South America

PostPosted: Tue Apr 22, 2025 1:57 pm    Post subject: Reply with quote

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
View user's profile Send private message
Marcofras
n00b
n00b


Joined: 29 May 2023
Posts: 52

PostPosted: Tue Apr 22, 2025 2:00 pm    Post subject: Reply with quote

solved there wasnt problem i was bad data reading.
thanks and bye
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum