0

The if statement does not run although it is True.

So I wrote a code in witch takes all the nums between 2 digits and adds them to two equations and gets the output. When the output is 3 and 1 it should print "This one" implying that it is the answer else it just says nope and prints the false value. The thing is, it prints nope even when the value is correct and for some reasons the of statement doesn't run even tho it is true when i = 1.8 and y = -0.2 (you can check that only then the equations values are 3 and 1) here is the code https://sololearn.com/compiler-playground/cR42zlj0TXhw/?ref=app

4th Dec 2023, 6:14 AM
Intermediate Depression
Intermediate Depression - avatar
10 Réponses
+ 1
floats and double are similar, just that doubles are capable of holding bigger values. The problem is that floating point values like these are inexact. Think of it this way: should 1.0 be equal to 0.999999? should it be equal to 1.000001? At what point are they not equal? What is close enough? These are questions a computer must consider when determining equality, but we did not give any specification, and the tolerance for equality might vary depending on use case. So doing value comparsion with floating point values is non trivial.
4th Dec 2023, 8:34 AM
Bob_Li
Bob_Li - avatar
+ 2
Bob_Li Alright so big question.....what is the difference between double and float? Looks pretty similar to me. Also I don't recall using float I only declared double!
4th Dec 2023, 8:25 AM
Intermediate Depression
Intermediate Depression - avatar
+ 1
The way your loop is incremented, the condition of n and o being 3 and 1 never simultaneously happen. Also value boolean comparison with double and floats never work. stick with integer, by maybe multiplying everything by 10? Try this test. The condition is always false, even when it seems it should be true. #include <iostream> using namespace std; int main() { for(double i=0;i<2;i+=0.1){ cout<<i<<' '<<(i==1.0)<<endl; if(i==1.0){ cout<<"i is 1\n"; } } }
4th Dec 2023, 7:09 AM
Bob_Li
Bob_Li - avatar
+ 1
Solving linear equations is commonly done with matrices and gaussian elimination.
4th Dec 2023, 8:36 AM
Bob_Li
Bob_Li - avatar
+ 1
Bob_Li c++ has matrix??? Also what is gaussian ? + how do I solve linear equations with them
4th Dec 2023, 8:46 AM
Intermediate Depression
Intermediate Depression - avatar
+ 1
matrix is just a fancy name for a 2d array. You put the coefficients of the equations into an array form. Gaussian elimination is a process of manipulating the array to basically do the computations to solve the equations. Google 'solving system of equations with c++'.
4th Dec 2023, 8:52 AM
Bob_Li
Bob_Li - avatar
+ 1
Intermediate Depression ok, clever workaround. It also means my assumption of n and o was wrong. comment out the else part to make the output cleaner. But it is still not a general solver and you somewhat pre-solved it by knowing the range of the solution beforehand. The matrix method can also handle more variables. So it's worth looking up.
4th Dec 2023, 9:05 AM
Bob_Li
Bob_Li - avatar
+ 1
Intermediate Depression Good for you. Trying things is a great way to learn. They might work, and they also often don't. But you learn a lot and that is important. Brute forcing an answer is something we all tried, one time or another. Not recommended, but yeah, better than not trying. If you have no idea what the solution is approximately, you would have to loop through very big ranges though... A pencil and paper and basic algebra would have been faster. But that's no fun 😁.
4th Dec 2023, 9:36 AM
Bob_Li
Bob_Li - avatar
+ 1
Bob_Li "But that's no fun " YES EXACTLY 😂 I feel understood on this matter for the first 😂😂❤❤
4th Dec 2023, 5:41 PM
Intermediate Depression
Intermediate Depression - avatar
0
Bob_Li Ok so ...look this is gonna sound super weird...but I fixed the code it is now working just as intended. But ....I actually don't understand why or how ..can you please explain to me why does it work now but it didn't work before (compared to now ) 😅😅😅😅😅
4th Dec 2023, 9:02 AM
Intermediate Depression
Intermediate Depression - avatar