Re: Reading specific memory address into variable

From: Gianni Mariani (gi2nospam_at_mariani.ws)
Date: 10/20/04


Date: Tue, 19 Oct 2004 21:26:51 -0700

David Casey wrote:
> I'm working on a program for my C++ class and have it all written and
> working except for one part. I need to compare two numeric variables to
> determine decimal accuracy between them. For example:
>
> pi = 3.14159...
> mynum = 3.14226...
>
> The mynum is accurate to 2 decimal places compared to pi. I could figure
> this out easy with a char array since I could just take a character one at
> a time from each array and compare it. However with a numeric variable I'm
> coming up short trying to figure this out.
>
> I've had other ideas, but I can't seem to make any of them work right so I
> figured I'd go back to my original idea which is to figure out where in
> memory the variable is stored using a pointer. Then read each digit in
> individually and compare it with the same digit in the same position in the
> other variable. If they are equal, add 1 to the precision variable. If
> they're not, then leave the loop and the accuracy is whatever precision is
> equal to.
>
> I've spent quite a few hours tonight looking in all the C++ books I own and
> searching both the Internet and Usenet but to no avail. I suppose I'm
> looking for a way to do what I was able to in BASIC all those years ago
> like the peek command which would return whatever was in a specific memory
> location.
>
> I'm not looking to be given an answer to my problem, just a nudge in the
> right direction so I can find it myself. Thanks to any help anyone can
> provide.

If a=0.000000123 and b=0.00000012499 is it accurate to 8 decimal places?

if a=123.1 and b=123.2 is it accurate to 0 decimal places?

Or is it more likely you care about accuracy to a number of significant
digits e.g.

- a=0.000000123 and b=0.00000012499 is accurate to 2 significant digits
- a=123.1 and b=123.2 is it accurate to 3 significant digits

Note that 1+int(log10 X) will give you the location of the first
significant digit of a number. Dividing a number by 10^(int(log10 X))
will give you a "normalized number", if take the normalized correct
answer and the normalized "guess" subtract them and find (-int(log10 X))
of the result, it will probably be very close to the number you're
looking for. You'll need to worry about sign (can't take log of
negative numbers too easily).

If you could get to the mantissa and exponent of the number you're
looking for, you could possibly make you're life very easy. Usually,
floating point numbers are already normalized, so (in most cases - not
all) simple difference of the mantissa and "find first set" will get you
an answer, except it will be in binary significant digits (which can be
converted to decimal significant digits by division by log10(2).

Oooh - frexp seems to do it.

#include <math>

int exp;
double mantissa = std::frexp( double(x), &exp );

When exp is at the limits of what the double can store, the mantissa is
not normalized, so you can simply call frexp twice and compare the
exponents to be equal both times. Then you can subtract the 2 resulting
mantissas and call std::frexp on the result and the exponent on the
third call will be the negative of the number of significant binary
digits which you can convert to decimal by the siggestion I made earlier.



Relevant Pages

  • Re: significant digits
    ... The number 0.0137 has how many significant digits? ... Consider rewriting all numbers in scientific notation, a mantissa ... non-zero numbers is non-zero. ...
    (sci.math)
  • Re: Two results of set geometry
    ... digits of pi and a subsequent 5 cannot be distinguished from pi as ... cannot compare it to pi because pi then does not and cannot exist. ... given enough time and stability of the representation and having ... Everything that exists, exists in physics, is limited by physics ...
    (sci.math)
  • Re: Int 32 processing speed: Is it a bug
    ... compare the exponent, shift one mantissa and *then* add the ... On a Pentium 4 for example, this takes 5 machine cycles ... vectorization police is never far away in this group. ...
    (comp.soft-sys.matlab)
  • Re: Divisibility of a java.math.BigInteger object
    ... and possibly faster to just extract the two least significant ... digits and compare them with »00«. ... To access the internal representation of a BigInteger use ... represenataion in decimal digits, though I suspect that will be too ...
    (comp.lang.java.programmer)
  • Re: Relative Cardinality
    ... > 90 additional digits are required to reach position 10^100. ... The number of digits needed to compare two magnitudes depends only on ... No one knows how much can be stored in the whole universe since no one ...
    (sci.math)