将特定内存地址读入变量 [英] Reading specific memory address into variable

查看:99
本文介绍了将特定内存地址读入变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的C ++课程编写一个程序并将其全部写完并且除了一部分之外还要工作
。我需要比较两个数值变量

确定它们之间的小数精度。例如:


pi = 3.14159 ...

mynum = 3.14226 ...


mynum是精确到pi的2位小数。因为我可以从每个数组中取出一个角色,然后从每个数组中获取一个角色,然后对它进行比较,我可以用char数组来计算这个很简单的
。然而,有了一个数字变量,我很想知道这个问题。


我有其他的想法,但我可以'似乎让他们中的任何一个都正常工作所以我

想我会回到我原来的想法,即找出

内存中的变量是什么使用指针存储。然后分别读取

中的每个数字,并将其与

其他变量中相同位置的相同数字进行比较。如果它们相等,则将1添加到精度变量。如果它们不是b $ b,那么就离开循环,准确度是等于
等于的精度。


我今晚我花了很多时间查看我拥有的所有C ++书籍和

搜索互联网和Usenet,但无济于事。我想我已经找到了一种方法来完成我多年前在BASIC中所做的工作

就像偷看命令那样可以返回任何东西特定记忆

位置。


我不希望得到我的问题的答案,只是在
$ b中轻推一下$ b正确的方向所以我可以自己找到它。感谢任何帮助,任何人都可以提供
.


Dave

-

你可以谈谈关于我们,但没有我们你就不能说话!

美国陆军通信兵团!!

http://www.geocities.com/davidcasey98

删除IH8SPAM以通过电子邮件回复!

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.

Dave
--
You can talk about us, but you can''t talk without us!
US Army Signal Corps!!

http://www.geocities.com/davidcasey98

Remove IH8SPAM to reply by email!

推荐答案

David Casey在新闻中写道:1w *************** @ sgtcaseycableone.net

comp.lang.c ++:
David Casey wrote in news:1w***************@sgtcaseycableone.net in
comp.lang.c++:
我不希望得到我的问题的答案,只是在正确的方向轻推,这样我就可以自己找。感谢任何人都能提供的任何帮助。
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.




好​​一些推动:


1)C ++ float,double和长的双重类型*不要*存储那些价值

作为一系列char'(至少在我听说过的任何平台上:)。


2)标准的iostreams库让你打印 float''到
stream'(参见#include< sstream>和std :: stringstream)。


3)还有std :: sprintf() - 但只有你喜欢生活

危险地:)


HTH。


Rob 。

-
http: //www.victim-prime.dsl.pipex.com/


2004年10月20日03:53:29 GMT,Rob Williscroft在

< news:Xn ********************************** @ 130.133 .1.4> ;:
On 20 Oct 2004 03:53:29 GMT, Rob Williscroft wrote in
<news:Xn**********************************@130.133 .1.4>:
好的一些推动:

1)C ++ float,double和long double类型*不要*存储值
一系列的char'(至少在我听说过的任何平台上:)。


啊,好的。我不敢相信我已经没有意识到这一点。谢谢!

2)标准的iostreams库让你打印 float''s to
stream'(参见#include< sstream>和std :: stringstream)。

3)还有std :: sprintf() - 但只有如果你喜欢危险地生活:)
Ok some nudges:

1) C++ float, double and long double types *don''t* store there values
as a sequence of char''s (at least on any platform I''ve heard about :).
Ah, okay. I can''t believe I didn''t realize that already. Thanks!
2) The standard iostreams library alows you to "print" float''s to
stream''s (see #include <sstream> and std::stringstream).

3) There is also std::sprintf() - but only if you like living
dangerously :)




这两个都比我现在的课程要先点一点。我可以

可能会把它们搞清楚,但是老师已经告诉我只要把我们在课堂上讨论过的程序放进去了。她现在正在谈论关于功能的
。 *叹气*


好​​吧,我现在还在修补我的问题的数学方法。

这可能就是答案所在。我只是不确定记忆

的方法,但是现在我看到它不会起作用我的想法。


谢谢!


Dave

谁已经知道如何使用功能。

-

你可以谈谈我们,但没有我们你就不能说话!

美国陆军通信兵团!!

http://www.geocities.com/davidcasey98

删除IH8SPAM以通过电子邮件回复!



Both of those are a bit ahead of the point my class is in now. I could
probably figure them out but the teacher has already told me to only put in
the programs what we''ve talked about in class. She is just now talking
about functions. *sigh*

Ah well, I''m still tinkering with a math approach to my problem right now.
That is probably where the answer is. I just wasn''t sure about the memory
approach, but now I see it won''t work how I was thinking.

Thanks!

Dave
Who already knows how to use functions.
--
You can talk about us, but you can''t talk without us!
US Army Signal Corps!!

http://www.geocities.com/davidcasey98

Remove IH8SPAM to reply by email!


David Casey写道:
David Casey wrote:
我正在为我的C ++课程编写一个程序并将其全部编写完成并且
工作除了一部分。我需要比较两个数字变量来确定它们之间的小数精度。例如:

pi = 3.14159 ...
mynum = 3.14226 ...

与pi相比,mynum精确到小数点后2位。我可以用char数组来轻松搞定这个,因为我可以从每个数组中取出一个角色,并进行比较。然而,有了一个数字变量我很想知道这个问题。

我有其他的想法,但我似乎无法做出任何一个工作正确,所以我想我会回到我原来的想法,即找出使用指针存储变量的内存。然后单独读取
中的每个数字,并将其与
其他变量中相同位置的相同数字进行比较。如果它们相等,则将1添加到精度变量。如果他们不是,那么就离开循环,准确度是等于什么精度。

今晚我花了很多时间查看所有内容我拥有的C ++书籍和搜索互联网和Usenet但无济于事。我想我正在寻找一种方法来做我多年前在BASIC中所做的事情
就像peek命令那样可以返回特定内存中的任何位置。

我不希望得到我的问题的答案,只是在正确的方向轻推,这样我就可以自己找到它。感谢任何人都能提供的任何帮助。
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.




如果a = 0.000000123且b = 0.00000012499,它是否精确到小数点后8位?


如果a = 123.1且b = 123.2是否精确到小数点后0位?


或者你更关心一些重要的精确度/>
位数例如


- a = 0.000000123和b = 0.00000012499精确到2位有效数字

- a = 123.1和b = 123.2它是否准确到3位有效数字


注意1 + int(log10 X)会给你一个数字的第一个

有效数字的位置。将数字除以10 ^(int(log10 X))

将为您提供标准化数字,如果采用标准化正确的

答案和标准化的猜"减去它们并找到(-int(log10 X))

的结果,它可能非常接近你要找的b
。你需要担心签名(不能太容易记录

负数)。


如果你能得到尾数和你想要的数字的指数,你可能会让你很轻松。通常,

浮点数已经标准化,因此(在大多数情况下 - 不是全部)b =尾数和找到第一组的简单差异。会给你

一个答案,除了它将是二进制有效数字(可以通过除以log10(2)将
转换为十进制有效数字。


哦 - frexp好像是这样做的。


#include< math>


int exp;

双尾数= std :: frexp(double(x),& exp);


当exp处于double可以存储的极限时,尾数是

没有标准化,所以你可以简单地调用两次frexp并比较

指数两次相等。然后你可以减去2得到的结果
mantissas并在结果上调用std :: frexp并且

第三次调用的指数将是有效二元数量的负数

位数你可以通过我之前做的siggestion转换为十进制。



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.


这篇关于将特定内存地址读入变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆