0

Okay so the title may not make the most sense but I will explain it better here.. what i'm trying to do is take the value of 2 and make it equal to .01 and make .01 = 2, well I tried to hardcode it like this

 if (Value = 2)
    Value = 0;
else if (Value = 1.99)
    Value = .01;
else if (Value = 1.98)
    Value = .02;
else if (Value = 1.97)
    Value = .03;
else if (Value = 1.96)
    Value = .04;
else if (Value = 1.95)
    Value = .05;

(I did that all the way down to 0... however it didn't function properly

I also tried to get the inverse of the value like so..

Value = 1 / Value;

Now if the value was 2 it would return it to .5 and if the value was 1 it would set it to 1.. I knew this wouldn't work when I tried I just didn't know what to do... If anyone could lead me in the right direction that would be cool.. (also I think this thread is a little bit better than my past threads seeing how I explained it :^) )

  • 1
    `=` is assignment. `==` is equality comparison. `if (Value = 2)` doesn't do what you likely think it does. – Igor Tandetnik Nov 05 '17 at 04:15
  • 3
    You wrote 200 if-else-if statements?! That's a sure sign there must be an easier way to do it. Like in this case, how about `Value = 2 - Value;`? – aschepler Nov 05 '17 at 04:18
  • 1
    also, you probably don't want to do `==` with floats, [see here](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – kmdreko Nov 05 '17 at 04:19
  • there are so many problems with your code. Read all the compiler warnings first. There would be a lot of warnings about variable assignment in the `if` block. And you can't compare floating-point values simply like that unless you really know what you're doing – phuclv Nov 05 '17 at 05:15

1 Answers1

3

You're using the assignment operator "=". This assigns a value to a variable (such as x = 10). Inside your if-check clause, you want to compare both variables equality with the compare-equality operator, "==".

if (Value == 2.f) // checks if Value is equal to 2
    Value = 0.f; // sets Value equal to 0
else if (Value == 1.99f)
    Value = .01f;
else if (Value == 1.98f)
    Value = .02f;
else if (Value == 1.97f)
    Value = .03f;
else if (Value == 1.96f)
    Value = .04f;
else if (Value == 1.95f)
    Value = .05f;

An alternative yet complete solution to your problem:

if (Value <= 2.f && Value >= 0.f) // checks if Value is between or is 2 or 0
    Value = 2.f - Value; // sets !version of Value to Value

Value = 1 / Value; wouldn't work because you're trying to get the !version of Value, not the inversion of Value.

Remember to do f after a float check so you don't run into weird faulty comparison problems.

  • That `f` doesn’t avoid “weird faulty comparison problems”. It only changes the type of the constant from `double` to `float`. If floating-point comparisons seem weird and faulty, that’s the result of not understanding how they work and where the data comes from. – Pete Becker Nov 05 '17 at 12:25