Re: The question of equality,...
- To: mathgroup at smc.vnet.net
- Subject: [mg60501] Re: The question of equality,...
- From: Peter Pein <petsie at dordos.net>
- Date: Sun, 18 Sep 2005 01:15:56 -0400 (EDT)
- References: <dggehc$gmv$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
terryisnow at yahoo.com schrieb:
> Ok I thought I understood this stuff but maybe not,...
>
> If I wanted to check for bit-for-bit equality of two
> arbitrary precision numbers (without regard to how
> many of those digits are meaningful) like the way it
> would be done using the "==" operator in C, then
> neither "==" nor "===" will do the job, is that
> correct?
>
> For "==" the 5.1 Appendix A says:
>
> "Approximate numbers are considered equal if they
> differ in at most their last eight binary digits
> (roughly their last two decimal digits)."
>
> For "===" the 5.1 Appendix A says:
>
> "SameQ requires exact correspondence between
> expressions, except that it considers Real numbers
> equal if their difference is less than the uncertainty
> of either of them"
>
> So what can I use then?
>
> Any and all help is much appreciated.
>
> Terry
>
Hi Terry,
RealDigits[] does what you want:
x1 = 1.;
x2 = x1*(1 + 10^(-Floor[$MachinePrecision]));
x1 == x2
True
RealDigits[#,10,Ceiling[$MachinePrecision]]&/@{x1,x2}
{{{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},1},
{{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},1}}
or bit-for-bit:
RealDigits[#,2,Ceiling[$MachinePrecision*Log[2,10]]]&/@{x1,x2}
{{{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},1},
{{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1},1}}
--
Peter Pein, Berlin
GnuPG Key ID: 0xA34C5A82
http://people.freenet.de/Peter_Berlin/