PDA

View Full Version : write variables



Striker47
05-21-2001, 10:39 AM
I tried to write a variable wch ist going bigger and smaller. I used tis code. But the Variable has alywase a size of 1.0 .
Why?
And how can i create a variable, wich i going bigger and smaller?

float groesse = 0.0;
bool trigger = true;


if (trigger = true)
{
groesse += 0.01;
if (groesse = 1.0)
{
trigger = false;
}
}
if (trigger = false)
{
groesse -= 0.01;
if (groesse = 0.0)
{
trigger = true;
}
}

sorry for my bad English

Randolph
05-21-2001, 10:44 AM
I think, you are not a C++ professional. When comparing numbers you should use == operator!

When you type "=" , it means assignment operator, not comparing. This is one of the most common bugs on C++.

[This message has been edited by Randolph (edited 05-21-2001).]

Randolph
05-21-2001, 10:46 AM
More than a half of people on OpenGL.org are not from English-speaking countries. So, dont excuse, you aren't the first.

Striker47
05-21-2001, 10:50 AM
@ Randolph:

What are you thinking, why I postet it into the "BEGINNER" Forum??

Striker47
05-21-2001, 10:55 AM
ok, I changed the code into this:


if (trigger == true)
{
groesse += 0.01;
if (groesse == 1.0)
{
trigger = false;
}
}
if (trigger == false)
{
groesse -= 0.01;
if (groesse == 0.0)
{
trigger = true;
}
}


But ..... It doesn't work. The variable "groesse" is alywas going to be bigger and bigger, but not going to be smaller, when 1.0 is reached.
Can somebody say me why?

Randolph
05-21-2001, 11:09 AM
I see, that you want following:
Variable groesse(i think, this is german for bigger) is 0 at the beginning. Then it increases to 1 and then decreases to 0 again.
The step is 0.01. That's my code for that:

float groesse = 0
while(groesse < 1.0)
{
groesse+=0.01;
//You may do something else after it
}
while(groesse > 0.0)
{
groesse-=0.01;
//Something else
}

Hope, this will help. If I rightly understood the idea.
I used while loops, because you cant make it work without them. Your code increases groesse by 0.01 but not by 1! So it is always less 1

Striker47
05-21-2001, 11:17 AM
Thanke you!!!

I want to use it to create a box, which is getting bigger and smaller.
THX

Randolph
05-21-2001, 11:26 AM
Dont be angry and excuse me, but I suggest you to read some books on C++ programming. You will waste much less time asking such questions.

Bob
05-21-2001, 12:13 PM
Since you increase the variable by 0.01 I assume you declared the variable as as float or a double. The problem is that these datatypes does not have infinite precision. First, you assign the value of 0.0 to the variable. Then, you increase the value by 0.01, or at least you think you do. But I can tell you this: you don't. 0.01 cannot be represented exactly, neither by a float nor a double. You increase it with a number very close to 0.01. After increasing the variable by 0.01 100 times, you assume it will contain 1.0, but it doesn't. It contains a number very close to 1.0, but not exactly 1.0.

A rule: do never compare a float/double with ==.

Gavin
05-22-2001, 01:02 AM
Bob,
In the case of comparing a say float and int when you assign
float f=1.0;
int i=1;

from what you say f != i
I have never thought about it before.....
And have never come across it..

gav

Bob
05-22-2001, 01:21 AM
In general, yes, that is true. But I have to say you chosed a very bad example. The fact is, MOST numbers can't be represented exactly, but SOME can, and 1.0 is one of them http://www.opengl.org/discussion_boards/ubb/smile.gif

I just made a simple test, here is some of the code.



int i=21;
float f=2.1;
printf("%i", ((float)i == (f*10.0))?1:0);

The code prints '0', so they are not equal, even thought they should be (in theory).

A tip if you want to compate two floats, is to see if one of them is "close enough" the other variable.

Gavin
05-22-2001, 01:35 AM
Yeah, I suppose the fact that I have never come across it is just a sign of my good code! :-)

Like randolph I always use <> or look at the diff, quite often I find myself needing an answer of 0 but would get *10^-100000 or whatever...

In fact one of our students asked if there was a bug in java as he was getting answers like that instead of 0 :-)

gav

Randolph
05-22-2001, 08:48 AM
In addition to what Bob said, I agree, that my code above did one more iteration, than it should. Really, it is much less risky not to use float as a loop variable.

The guy tried to use if's to organize loops, I think, that he had to launch that code many times. His critical mistake was not using == operator but not floating point comparing. As he said, he needed box, which size was variable groesse. So, I dont think it was critical for him, if his code did one more iteration, than it should.

Thug
05-29-2001, 09:53 AM
About comparing floats and doubles to each other... Depending, if at anytime in your calculations, the decimal part of the float or double is smaller than .000001 or larger than -.000001.. you are asking for trouble. As an assignment I had to take the number 1.0f and divide it by 2 and then take the result and divide that by 2 and so fourth. Trying to figure out when dividing by 2 made the percision useless.

bsperlin
05-30-2001, 04:43 AM
If you are worried about small imprecisions in floats and doubles then use >= instead of ==, such as in your original code:

if (trigger == true)
{
groesse += 0.01;
if (groesse >= 1.0)
{
trigger = false;
}

Now if your float registers 1 as 1.000001 then this still works.
Barry

bsperlin
05-30-2001, 04:44 AM
If you are worried about small imprecisions in floats and doubles then use >= instead of ==, such as in your original code:

if (trigger == true)
{
groesse += 0.01;
if (groesse >= 1.0)
{
trigger = false;
}

Now if your float registers 1 as 1.000001 then this still works.
Barry

Deiussum
05-30-2001, 05:39 AM
Just thought I'd throw in a quick explanation of why floats and doubles are inexact for a lot of things. I won't go into the full details of how the IEEE format is setup but think of decimal points in a binary number for a moment..

(crossing my fingers and hoping this formats correctly.) http://www.opengl.org/discussion_boards/ubb/smile.gif



2's place
| 1's place
| |
1 0.1 0 1 1
****| | | |
****| | | 1/16 place
****| | 1/8 place
****| 1/4 place
****1/2 place


Now try to find a way to represent the decimal value 0.1 (1/10) in binary. You can't. You can do 1/16 (0.0001) or 1/8 (0.001), but nothing in between like 1/10.



[This message has been edited by Deiussum (edited 05-30-2001).]