473,491 Members | 2,074 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

floating point problems?

Hi,

does repeatingly doing this:

float num = GetRandomFloat();
for(;;)
{
float random = GetRandomFloat();
num*=random;
num/=random;
}

Will num stay the same?
What can I do to make it stay the same (FixFloat(&random) e.g?)

--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}

________________________________________
Looking for a good game? Do it yourself!
GLBasic - you can do
www.GLBasic.com
Jul 22 '05 #1
17 1681
Gernot Frisch wrote:
Hi,

does repeatingly doing this:

float num = GetRandomFloat();
for(;;)
{
float random = GetRandomFloat();
num*=random;
num/=random;
}


What about when num*random > maximum float?
Also, random == 0.0 gives you a floating division by zero.

And num will not stay the same because of precision losses.

- J.
Jul 22 '05 #2

"Jacek Dziedzic" <ja*************@janowo.net> schrieb im Newsbeitrag
news:cn**********@korweta.task.gda.pl...
Gernot Frisch wrote:
Hi,

does repeatingly doing this:

float num = GetRandomFloat();
for(;;)
{
float random = GetRandomFloat();
num*=random;
num/=random;
}
What about when num*random > maximum float?
Also, random == 0.0 gives you a floating division by zero.


Taken care of that.
And num will not stay the same because of precision losses.


That's my question: Is there any way to make "random", so that
precision losses it not of any interest?
Jul 22 '05 #3
Gernot Frisch wrote:
"Jacek Dziedzic" <ja*************@janowo.net> schrieb im Newsbeitrag
news:cn**********@korweta.task.gda.pl...
Gernot Frisch wrote:
Hi,

does repeatingly doing this:

float num = GetRandomFloat();
for(;;)
{
float random = GetRandomFloat();
num*=random;
num/=random;
}


What about when num*random > maximum float?
Also, random == 0.0 gives you a floating division by zero.

Taken care of that.

And num will not stay the same because of precision losses.

That's my question: Is there any way to make "random", so that
precision losses it not of any interest?


What I don't understand is why you insist on first multiplying
num by random, only to divide it by random a moment later? It's
not clear for me what you are trying to do. Do you simply want
to generate random float numbers within a certain range? It seems
not, because supposedly GetRandomFloat() does this for you.
So what are you trying to achieve?

- J.
Jul 22 '05 #4

"Gernot Frisch" <Me@Privacy.net> wrote in message
news:2v*************@uni-berlin.de...

"Jacek Dziedzic" <ja*************@janowo.net> schrieb im Newsbeitrag
news:cn**********@korweta.task.gda.pl...
Gernot Frisch wrote:
Hi,

does repeatingly doing this:

float num = GetRandomFloat();
for(;;)
{
float random = GetRandomFloat();
num*=random;
num/=random;
}


What about when num*random > maximum float?
Also, random == 0.0 gives you a floating division by zero.


Taken care of that.
And num will not stay the same because of precision losses.


That's my question: Is there any way to make "random", so that precision
losses it not of any interest?


Yes

float num = GetRandomFloat();
for(;;)
{
float random = GetRandomFloat();
float save_num = num;
num*=random;
num = save_num;
}

Likely to be more efficient too, and clearer code.

john
Jul 22 '05 #5
Gernot Frisch wrote:

"Jacek Dziedzic" <ja*************@janowo.net> schrieb im Newsbeitrag
news:cn**********@korweta.task.gda.pl...
Gernot Frisch wrote:
Hi,

does repeatingly doing this:

float num = GetRandomFloat();
for(;;)
{
float random = GetRandomFloat();
num*=random;
num/=random;
}


What about when num*random > maximum float?
Also, random == 0.0 gives you a floating division by zero.


Taken care of that.
And num will not stay the same because of precision losses.


That's my question: Is there any way to make "random", so that
precision losses it not of any interest?


If your question is: Is there a way to get the benefits
of floating point arithmetic while avoiding the precission loss
then the answer is: no.

The reason is simple: A genious once proved that there is an infinite
amount of rational numbers between 0 and 1. You can't represent an infinite
amount of numbers with a finite amount of bits.
So the problem with floating point numbers is system imanent and there is
nothing you can do about it. Live with it.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #6
Gernot Frisch wrote:
Hi,

does repeatingly doing this:

float num = GetRandomFloat();
for(;;)
{
float random = GetRandomFloat();
num*=random;
num/=random;
What on earth is this supposed to achieve?
}

Will num stay the same?
No, because of loss of precision.
What can I do to make it stay the same (FixFloat(&random) e.g?)

Omit the lines:

num*=random;
num/=random;
--
Lionel B

Jul 22 '05 #7
Gernot Frisch wrote:

"Jacek Dziedzic" <ja*************@janowo.net> schrieb im Newsbeitrag
news:cn**********@korweta.task.gda.pl...
Gernot Frisch wrote:
Hi,

does repeatingly doing this:

float num = GetRandomFloat();
for(;;)
{
float random = GetRandomFloat();
num*=random;
num/=random;
}


What about when num*random > maximum float?
Also, random == 0.0 gives you a floating division by zero.


Taken care of that.
And num will not stay the same because of precision losses.


That's my question: Is there any way to make "random", so that
precision losses it not of any interest?


Make it double instead of float?

Seriously: Until you know exactly what you do AND you know what
is awaiting you AND you are willing and have the knowledge to fight
that beast AND there is not a very, very, very good reason: forget
that data type float exists and use double instead.

The 'floating point problem' is still there with double, but it
is much smaller. Small enough that with a little bit of care it
can be ignored in practice (except for comparisons, of corse).

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #8
>> float num = GetRandomFloat();
for(;;)
{
float random = GetRandomFloat();


num*=random;
num/=random;


What on earth is this supposed to achieve?


float scale = GetScalingForCartoonOutlines();
for(int i=0; i<afewthousandtriangles; ++i)
{
ScaleVertex(pVertices[i], scale);
}
glDrawElements(pVertices, afewthousandtriangles);
scale=1.0f/scale;
for(int i=0; i<afewthousandtriangles; ++i)
{
ScaleVertex(pVertices[i], scale);
}

The other way would be to have an additional "afewthousandtriangles"
array of data that I don't really need.

Thx anyway,
-Gernot
Jul 22 '05 #9
Gernot Frisch wrote:
float num = GetRandomFloat();
for(;;)
{
float random = GetRandomFloat();


num*=random;
num/=random;


What on earth is this supposed to achieve?


float scale = GetScalingForCartoonOutlines();
for(int i=0; i<afewthousandtriangles; ++i)
{
ScaleVertex(pVertices[i], scale);
}
glDrawElements(pVertices, afewthousandtriangles);
scale=1.0f/scale;
for(int i=0; i<afewthousandtriangles; ++i)
{
ScaleVertex(pVertices[i], scale);
}

The other way would be to have an additional "afewthousandtriangles"
array of data that I don't really need.


But you need it (see later).
It always a good idea to reserve memory for temporary results
if you then can avoid modifying the originals.

As for your specific problem. It is alwas a good idea
to know the toolkit you are working with.

glPushMatrix();
glScalef( 1.0 / scale, 1.0 / scale, 1.0 / scale );
glDrawElements( pVertices, afewthousandtriangles );
glPopMatrix();

Does the very same as your mumbo jumbo 2 loop solution.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #10
Karl Heinz Buchegger wrote:

Gernot Frisch wrote:
> float num = GetRandomFloat();
> for(;;)
> {
> float random = GetRandomFloat();
> num*=random;
> num/=random;

What on earth is this supposed to achieve?
float scale = GetScalingForCartoonOutlines();
for(int i=0; i<afewthousandtriangles; ++i)
{
ScaleVertex(pVertices[i], scale);
}
glDrawElements(pVertices, afewthousandtriangles);
scale=1.0f/scale;
for(int i=0; i<afewthousandtriangles; ++i)
{
ScaleVertex(pVertices[i], scale);
}

The other way would be to have an additional "afewthousandtriangles"
array of data that I don't really need.


But you need it (see later).
It always a good idea to reserve memory for temporary results
if you then can avoid modifying the originals.

As for your specific problem. It is alwas a good idea
to know the toolkit you are working with.

glPushMatrix();
glScalef( 1.0 / scale, 1.0 / scale, 1.0 / scale );


Sorry, obviously must read:

glScalef( scale, scale, scale );

glDrawElements( pVertices, afewthousandtriangles );
glPopMatrix();

Does the very same as your mumbo jumbo 2 loop solution.


--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #11
glScalef( scale, scale, scale );
Does the very same as your mumbo jumbo 2 loop solution.


Unfortunately not, I've got to scale the vetices along their normal
directions, which makes this a bit expensive. I've decided to
implement a temp-buffer for each object now, thank you.
Jul 22 '05 #12
Gernot Frisch wrote:
glScalef( scale, scale, scale );
Does the very same as your mumbo jumbo 2 loop solution.


Unfortunately not, I've got to scale the vetices along their normal
directions, which makes this a bit expensive. I've decided to
implement a temp-buffer for each object now, thank you.


OK. If the center of scale is not identical for all points,
then thats the best solution.

That's one of the great principles in graphics:
Don't accumulate errors!

Eg. you want to rotate something in 0.1 degree steps for
a full circle:

Don't do

for( double alpha = 0; alpha < 360.0; alpha += 0.1 ) {
// apply the angle

But do:

for( int i = 0; i < 3600; ++i )
alpha = i * 0.1;

So why is the second better?
Because in the first loop, the error of the first addition
will be distibuted to the second addition which in turn will
influence the 3-rd addition etc. All in all you are doing 3600
additions and in the end you will have accumulated a lot of error.

"Working with floating point numbers is like moving piles of sand.
Every time you do it, you loose a little sand and pick up a little
dirt"

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #13
Karl Heinz Buchegger wrote:
Gernot Frisch wrote:
glScalef( scale, scale, scale );

Does the very same as your mumbo jumbo 2 loop solution.


Unfortunately not, I've got to scale the vetices along their normal
directions, which makes this a bit expensive. I've decided to
implement a temp-buffer for each object now, thank you.

OK. If the center of scale is not identical for all points,
then thats the best solution.


Center of scale ? You mean the origin of the coordinate system, right ?
The origin has no relation to the normal vectors, because the do not
change under translation. The problem I believe is that if you scale
normal vectors, you enable Normalization, which is expensive.

-Arijit
Jul 22 '05 #14
Karl Heinz Buchegger wrote:

"Working with floating point numbers is like moving piles of sand.
Every time you do it, you loose a little sand and pick up a little
dirt"


Now, that's a nice proverb! :)

- J.
Jul 22 '05 #15
OK. If the center of scale is not identical for all points,
then thats the best solution.


Center of scale ? You mean the origin of the coordinate system,
right ? The origin has no relation to the normal vectors, because
the do not change under translation. The problem I believe is that
if you scale normal vectors, you enable Normalization, which is
expensive.


Nah. Thin of a donut. When you scale it, the inner rings will go
further from the center, right? Now scale each vertex point along it's
normal direction. You get a blown-up donut this way.
Jul 22 '05 #16
Arijit wrote:

Karl Heinz Buchegger wrote:
Gernot Frisch wrote:
glScalef( scale, scale, scale );

>Does the very same as your mumbo jumbo 2 loop solution.

Unfortunately not, I've got to scale the vetices along their normal
directions, which makes this a bit expensive. I've decided to
implement a temp-buffer for each object now, thank you.

OK. If the center of scale is not identical for all points,
then thats the best solution.


Center of scale ? You mean the origin of the coordinate system, right ?


No. I mean 'center of scale'. The point that should be invariant under
the scale operation.
The origin has no relation to the normal vectors, because the do not
change under translation. The problem I believe is that if you scale
normal vectors, you enable Normalization, which is expensive.


I don't think that the OP has that problem.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #17
Gernot Frisch wrote:
OK. If the center of scale is not identical for all points,
then thats the best solution.


Center of scale ? You mean the origin of the coordinate system,
right ? The origin has no relation to the normal vectors, because
the do not change under translation. The problem I believe is that
if you scale normal vectors, you enable Normalization, which is
expensive.


Nah. Thin of a donut. When you scale it, the inner rings will go
further from the center, right? Now scale each vertex point along it's
normal direction. You get a blown-up donut this way.


I see. You are right. That cannot be done with a classical scale
operation. The classical scale operation assumes that the center
of scale is a single point, which it isn't in your case.
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #18

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3284
by: Dave | last post by:
Hi folks, I am trying to develop a routine that will handle sphere-sphere and sphere-triangle collisions and interactions. My aim is to develop a quake style collision engine where a player can...
13
2883
by: Dylan Nicholson | last post by:
I just posted regarding a possible floating point error (not sure where), and have since discovered that: float f = 5.15002; double d = 5.15002; if (float(d) < f) puts("huh 1?"); float f2 =...
10
2232
by: Shawn | last post by:
Hello all, I apologize as I am sure this has probably been dealth with before... but I am doing an exercise from "Practical C Programming" and I have been unable to get it to work perfectly due to...
25
3601
by: Gaurav Verma | last post by:
Hi, I want to convert a floating point number (or a decimal number say 123.456) into binary notation using a C program. Can somebody help me out with it? Thanks Gaurav --...
15
3891
by: michael.mcgarry | last post by:
Hi, I have a question about floating point precision in C. What is the minimum distinguishable difference between 2 floating point numbers? Does this differ for various computers? Is this...
5
3322
by: Peteroid | last post by:
I know how to use rand() to generate random POSITIVE-INTEGER numbers. But, I'd like to generate a random DOUBLE number in the range of 0.0 to 1.0 with resolution of a double (i.e., every possible...
4
2815
by: jacob navia | last post by:
Hi people I continue to work in the tutorial for lcc-win32, and started to try to explain the floating point flags. Here is the relevant part of the tutorial. Since it is a difficult part, I...
70
3517
by: Robert Gamble | last post by:
9899:1999 5.1.2.3 Example 4 reads: "EXAMPLE 4 Implementations employing wide registers have to take care to honor appropriate semantics. Values are independent of whether they are represented in a...
1
2744
by: Shhnwz.a | last post by:
Hi, I have a problem regarding handling floating point error, specially Denormalisation error. I want to know is there any trick or technique to find bit length of the resultant before using and...
5
3059
by: Keflavich | last post by:
Hey, I have a bit of code that died on a domain error when doing an arcsin, and apparently it's because floating point subtraction is having problems. I know about the impossibility of storing...
0
6978
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6858
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5451
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4881
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4578
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3086
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3076
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1392
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
633
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.