Board index » Visual Studio » Will the following code assert:

Will the following code assert:

Visual Studio320
// headers...



int main ()

{

double d = 0;

ASSERT (d == 0);

}



?



Or is it machine/[HW/SW]floating-point processing dependent? Might 1.0E-1024

be a convenient way to store zero on some machines? Is there any certainty

with doubles of floats or do we 100% of the time have to call fabs with some

precision spec?





Thanks in advance,

- Alan


-
 

Re:Will the following code assert:

Err, more precisely does it always have to be:



ASSERT (fabs (d - 0) < fprecision).



ie. can we ever use "==" with doubles/floats? Or is it completely

meaningless to ask if two doubles are "equal".



- Alan





"Alan Carre" <alan@twilightgames.com>wrote in message

Quote
// headers...



int main ()

{

double d = 0;

ASSERT (d == 0);

}



?



Or is it machine/[HW/SW]floating-point processing dependent? Might

1.0E-1024 be a convenient way to store zero on some machines? Is there any

certainty with doubles of floats or do we 100% of the time have to call

fabs with some precision spec?





Thanks in advance,

- Alan









-

Re:Will the following code assert:

Alan Carre wrote:

Quote
// headers...



int main ()

{

double d = 0;

ASSERT (d == 0);

}



?



Or is it machine/[HW/SW]floating-point processing dependent? Might 1.0E-1024

be a convenient way to store zero on some machines? Is there any certainty

with doubles of floats or do we 100% of the time have to call fabs with some

precision spec?



double a = 42;

double b = 3.14159;

a = b;

if (a == b) // should be just like if (true)



Two floating point values shall always compare equal with '==' if they are

(a) of the same FP type and (b) one was assigned from the other. If the

conditions are different, then '==' may not be the right choice.



There is always a possibility of a bug in the compiler, of course. Slim,

but not necessarily altogether absent.



V

-

Re:Will the following code assert:

I cannot imagine a properly functioning compiler translating "0" into

one binary image on one line, and a different binary image on the next.

Hence we can feel confident that the following will run (without an abort):

int main ()

{

double d = 0;

double e = 0;

ASSERT (memcmp(d,e,sizeof(double)) == 0);

}







--

Truth,

James Curran

[erstwhile VC++ MVP]



Home: www.noveltheory.com Work: www.njtheater.com

Blog: www.honestillusion.com Day Job: www.partsearch.com



"Alan Carre" <alan@twilightgames.com>wrote in message

Quote
// headers...



int main ()

{

double d = 0;

ASSERT (d == 0);

}



?



Or is it machine/[HW/SW]floating-point processing dependent? Might

1.0E-1024

be a convenient way to store zero on some machines? Is there any certainty

with doubles of floats or do we 100% of the time have to call fabs with

some

precision spec?





Thanks in advance,

- Alan









-

Re:Will the following code assert:

IEEE standard floats have a defined bit pattern. Zero is _defined_ as all

bits off, so your assert would pass without an assertion. This is a +0,

btw. There is also a -0 where the bit pattern is not all zero.



This may or may not be true on platforms that use other floating point

representations.



What _is_ dangerous is to assume that a series of calculations that

mathematically yield a result of zero will be stored as zero in your float.

Your assert will probably fail in these cases.





I'm not sure it testing -0 against 0.0 with an equality test would produce

equality or not....







"Alan Carre" <alan@twilightgames.com>wrote in message

Quote
// headers...



int main ()

{

double d = 0;

ASSERT (d == 0);

}



?



Or is it machine/[HW/SW]floating-point processing dependent? Might

1.0E-1024

be a convenient way to store zero on some machines? Is there any certainty

with doubles of floats or do we 100% of the time have to call fabs with

some

precision spec?





Thanks in advance,

- Alan









-

Re:Will the following code assert:

"Joe Butler" <ffffh.no.spam@hotmail-spammers-paradise.com>wrote in

message news:ubQsyV87FHA.1000@tk2msftngp13.phx.gbl

Quote
I'm not sure it testing -0 against 0.0 with an equality test would

produce equality or not....



If a platform follows IEEE standard, then +0 and -0 must be considered

equal.

--

With best wishes,

Igor Tandetnik



With sufficient thrust, pigs fly just fine. However, this is not

necessarily a good idea. It is hard to be sure where they are going to

land, and it could be dangerous sitting under them as they fly

overhead. -- RFC 1925





-

Re:Will the following code assert:

Thanks all for your answers. I will assume assignment to zero will result in

"== 0" being true.



- Alan



"Alan Carre" <alan@twilightgames.com>wrote in message

Quote
// headers...



int main ()

{

double d = 0;

ASSERT (d == 0);

}



?



Or is it machine/[HW/SW]floating-point processing dependent? Might

1.0E-1024 be a convenient way to store zero on some machines? Is there any

certainty with doubles of floats or do we 100% of the time have to call

fabs with some precision spec?





Thanks in advance,

- Alan









-

Re:Will the following code assert:

Assigning from a constant 0 should work since most floating point

schemes can represent 0 exactly. However, assigning from an

expression that you believe evaluates to 0, such as

d = 2 - sqrt(2)*sqrt(2);

can produce non-zero values.



On Thu, 24 Nov 2005 22:52:52 +0700, "Alan Carre"

<alan@twilightgames.com>wrote:



Quote
Thanks all for your answers. I will assume assignment to zero will result in

"== 0" being true.



- Alan



"Alan Carre" <alan@twilightgames.com>wrote in message

news:ebQAKK47FHA.252@TK2MSFTNGP15.phx.gbl...

>// headers...

>

>int main ()

>{

>double d = 0;

>ASSERT (d == 0);

>}

>

>?

>

>Or is it machine/[HW/SW]floating-point processing dependent? Might

>1.0E-1024 be a convenient way to store zero on some machines? Is there any

>certainty with doubles of floats or do we 100% of the time have to call

>fabs with some precision spec?

>

>

>Thanks in advance,

>- Alan

>

>







<<Remove the del for email>>

-

Re:Will the following code assert:

Sure of course. I meant only assignment to 0 itself.



- Alan



"Barry Schwarz" <schwarzb@deloz.net>wrote in message

Quote
Assigning from a constant 0 should work since most floating point

schemes can represent 0 exactly. However, assigning from an

expression that you believe evaluates to 0, such as

d = 2 - sqrt(2)*sqrt(2);

can produce non-zero values.



On Thu, 24 Nov 2005 22:52:52 +0700, "Alan Carre"

<alan@twilightgames.com>wrote:



>Thanks all for your answers. I will assume assignment to zero will result

>in

>"== 0" being true.

>

>- Alan

>

>"Alan Carre" <alan@twilightgames.com>wrote in message

>news:ebQAKK47FHA.252@TK2MSFTNGP15.phx.gbl...

>>// headers...

>>

>>int main ()

>>{

>>double d = 0;

>>ASSERT (d == 0);

>>}

>>

>>?

>>

>>Or is it machine/[HW/SW]floating-point processing dependent? Might

>>1.0E-1024 be a convenient way to store zero on some machines? Is there

>>any

>>certainty with doubles of floats or do we 100% of the time have to call

>>fabs with some precision spec?

>>

>>

>>Thanks in advance,

>>- Alan

>>

>>

>





<<Remove the del for email>>





-

Re:Will the following code assert:

Err... what do you mean by *most* floating point schemes? If it's not all,

then I must throw away my assumption and use 'fabs' and '>'.



ie.



if (fabs (xvelocity)>0.00001)

{

//do lots of work.

}

else

{

//do nothing.

}



What's a good number to choose? 0.00001 seems pretty good assuming it's

"pixels per second". What about a more general case? How low can you go that

is, and still be relatively 'safe'???



- A



"Barry Schwarz" <schwarzb@deloz.net>wrote in message

Quote
Assigning from a constant 0 should work since most floating point

schemes can represent 0 exactly. However, assigning from an

expression that you believe evaluates to 0, such as

d = 2 - sqrt(2)*sqrt(2);

can produce non-zero values.



On Thu, 24 Nov 2005 22:52:52 +0700, "Alan Carre"

<alan@twilightgames.com>wrote:



>Thanks all for your answers. I will assume assignment to zero will result

>in

>"== 0" being true.

>

>- Alan

>

>"Alan Carre" <alan@twilightgames.com>wrote in message

>news:ebQAKK47FHA.252@TK2MSFTNGP15.phx.gbl...

>>// headers...

>>

>>int main ()

>>{

>>double d = 0;

>>ASSERT (d == 0);

>>}

>>

>>?

>>

>>Or is it machine/[HW/SW]floating-point processing dependent? Might

>>1.0E-1024 be a convenient way to store zero on some machines? Is there

>>any

>>certainty with doubles of floats or do we 100% of the time have to call

>>fabs with some precision spec?

>>

>>

>>Thanks in advance,

>>- Alan

>>

>>

>





<<Remove the del for email>>





-

Re:Will the following code assert:

If anyone cares... experimentation reveals these limits:



float f = 0;

ASSERT (f == 0);



f = 0.0000000000000000000000000000000000000000000008;

ASSERT (f != 0);



f = 0.0000000000000000000000000000000000000000000007;

ASSERT (f == 0);



Code does not assert. It seems the 'smallest positive float' is

1.40130e-045, anything smaller seems to zero-fill the buffer (which appears

to have the effect of dropping the exponent).



- Alan



"Barry Schwarz" <schwarzb@deloz.net>wrote in message

Quote
Assigning from a constant 0 should work since most floating point

schemes can represent 0 exactly. However, assigning from an

expression that you believe evaluates to 0, such as

d = 2 - sqrt(2)*sqrt(2);

can produce non-zero values.



On Thu, 24 Nov 2005 22:52:52 +0700, "Alan Carre"

<alan@twilightgames.com>wrote:



>Thanks all for your answers. I will assume assignment to zero will result

>in

>"== 0" being true.

>

>- Alan

>

>"Alan Carre" <alan@twilightgames.com>wrote in message

>news:ebQAKK47FHA.252@TK2MSFTNGP15.phx.gbl...

>>// headers...

>>

>>int main ()

>>{

>>double d = 0;

>>ASSERT (d == 0);

>>}

>>

>>?

>>

>>Or is it machine/[HW/SW]floating-point processing dependent? Might

>>1.0E-1024 be a convenient way to store zero on some machines? Is there

>>any

>>certainty with doubles of floats or do we 100% of the time have to call

>>fabs with some precision spec?

>>

>>

>>Thanks in advance,

>>- Alan

>>

>>

>





<<Remove the del for email>>





-

Re:Will the following code assert:

Check out FLT_EPSILON, FLT_MIN, etc. in <float.h>





"Alan Carre" <alan@twilightgames.com>wrote in message

Quote
If anyone cares... experimentation reveals these limits:



float f = 0;

ASSERT (f == 0);



f = 0.0000000000000000000000000000000000000000000008;

ASSERT (f != 0);



f = 0.0000000000000000000000000000000000000000000007;

ASSERT (f == 0);



Code does not assert. It seems the 'smallest positive float' is

1.40130e-045, anything smaller seems to zero-fill the buffer (which

appears

to have the effect of dropping the exponent).



- Alan



"Barry Schwarz" <schwarzb@deloz.net>wrote in message

news:jl4co1pftupfrq985ektuua28ekdrk6jjm@4ax.com...

>Assigning from a constant 0 should work since most floating point

>schemes can represent 0 exactly. However, assigning from an

>expression that you believe evaluates to 0, such as

>d = 2 - sqrt(2)*sqrt(2);

>can produce non-zero values.

>

>On Thu, 24 Nov 2005 22:52:52 +0700, "Alan Carre"

><alan@twilightgames.com>wrote:

>

>>Thanks all for your answers. I will assume assignment to zero will

result

>>in

>>"== 0" being true.

>>

>>- Alan

>>

>>"Alan Carre" <alan@twilightgames.com>wrote in message

>>news:ebQAKK47FHA.252@TK2MSFTNGP15.phx.gbl...

>>>// headers...

>>>

>>>int main ()

>>>{

>>>double d = 0;

>>>ASSERT (d == 0);

>>>}

>>>

>>>?

>>>

>>>Or is it machine/[HW/SW]floating-point processing dependent? Might

>>>1.0E-1024 be a convenient way to store zero on some machines? Is there

>>>any

>>>certainty with doubles of floats or do we 100% of the time have to

call

>>>fabs with some precision spec?

>>>

>>>

>>>Thanks in advance,

>>>- Alan

>>>

>>>

>>

>

>

><<Remove the del for email>>









-

Re:Will the following code assert:

On Sat, 26 Nov 2005 06:55:06 +0700, "Alan Carre"

<alan@twilightgames.com>wrote:



Quote
Err... what do you mean by *most* floating point schemes? If it's not all,

then I must throw away my assumption and use 'fabs' and '>'.



I mean that there are lots of systems that I have not used and it

would be presumptuous of me to assume they behave like the ones I am

familiar with. I learned a long time ago that others do not suffer

the same limits of imagination that I do and my limits don't seem to

be related to the real world.



Quote


ie.



if (fabs (xvelocity)>0.00001)

{

//do lots of work.

}

else

{

//do nothing.

}



What's a good number to choose? 0.00001 seems pretty good assuming it's

"pixels per second". What about a more general case? How low can you go that

is, and still be relatively 'safe'???



- A



"Barry Schwarz" <schwarzb@deloz.net>wrote in message

news:jl4co1pftupfrq985ektuua28ekdrk6jjm@4ax.com...

>Assigning from a constant 0 should work since most floating point

>schemes can represent 0 exactly. However, assigning from an

>expression that you believe evaluates to 0, such as

>d = 2 - sqrt(2)*sqrt(2);

>can produce non-zero values.

>

>On Thu, 24 Nov 2005 22:52:52 +0700, "Alan Carre"

><alan@twilightgames.com>wrote:

>

>>Thanks all for your answers. I will assume assignment to zero will result

>>in

>>"== 0" being true.

>>

>>- Alan

>>

>>"Alan Carre" <alan@twilightgames.com>wrote in message

>>news:ebQAKK47FHA.252@TK2MSFTNGP15.phx.gbl...

>>>// headers...

>>>

>>>int main ()

>>>{

>>>double d = 0;

>>>ASSERT (d == 0);

>>>}

>>>

>>>?

>>>

>>>Or is it machine/[HW/SW]floating-point processing dependent? Might

>>>1.0E-1024 be a convenient way to store zero on some machines? Is there

>>>any

>>>certainty with doubles of floats or do we 100% of the time have to call

>>>fabs with some precision spec?

>>>

>>>

>>>Thanks in advance,

>>>- Alan

>>>

>>>

>>

>

>

><<Remove the del for email>>







<<Remove the del for email>>

-