Re: Reading specific memory address into variable
From: Gianni Mariani (gi2nospam_at_mariani.ws)
Date: 10/20/04
- Next message: Rob Williscroft: "Re: namespaces conflict?"
- Previous message: Sharad Kala: "Re: pointer to structure?"
- In reply to: David Casey: "Reading specific memory address into variable"
- Next in thread: David Casey: "Re: Reading specific memory address into variable"
- Reply: David Casey: "Re: Reading specific memory address into variable"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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.
- Next message: Rob Williscroft: "Re: namespaces conflict?"
- Previous message: Sharad Kala: "Re: pointer to structure?"
- In reply to: David Casey: "Reading specific memory address into variable"
- Next in thread: David Casey: "Re: Reading specific memory address into variable"
- Reply: David Casey: "Re: Reading specific memory address into variable"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|