双 - >文字 - >双 [英] double -> text -> double

查看:101
本文介绍了双 - >文字 - >双的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,对不起,如果这是偏离主题,而不是严格意义上的C ++问题。

我找不到数字或序列化的数据,我认为

这个ng是我能得到的最接近的。


现在问题:


我想用人类可读的十进制格式序列化双打

并确保当我读回

时我得到完全相同的二进制值。 (现在,我不在乎NaN,无穷大等等)


在本质上,这归结为将双倍转换为

(大)整数尾数和小数指数,然后返回,

这样3.1416将表示为{31416,-4}。


我写了一个转换器总是单独计算比例

并且在阅读

并使用最多17位数的mantiassa写作时完全相同,这应该是

足够精确。然后我用脏测试它。数字

由三角函数和exp函数生成,并且发现

它似乎适用于+/- 20

范围内的指数但是在该范围之外,数字的百分之几是b / b
/ b
有没有人知道

工作?


我尝试了一种算法,当转换双>文本时,

会转换回双倍并尝试调整尾数

如果这个双倍与原版不同,但这只会让事情变得更糟。


问候/ Ole Nielsby

解决方案

Ole Nielsby写道:


首先,对不起,如果这是偏离主题,不是严格意义上的C ++问题。

我找不到数字或序列化的数字,我认为这是我最接近的



这已经足够了。每种语言都有一个解决方案,我猜,

但它会特定于语言。


现在问题:


我想用人类可读的十进制格式序列化双打

并确保在我阅读时得到完全相同的二进制值

他们背部。 (现在,我不在乎NaN,无穷大等。)



输出的数字多于''double''的精度。参见

''std :: numeric_limits''模板。


V

-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要问




Ole Nielsby写道:


我写了一个总是计算缩放比例的转换器

当你阅读

和写作时,使用多达17位的mantiassa,这应该是完全相同的,这应该是足够精确的。然后我用脏测试它。数字

由三角函数和exp函数生成,并且发现

它似乎适用于+/- 20

范围内的指数但是在这个范围之外,数字的百分之几是不同的数字。



你如何比较结果?


你如何从两个整数转换成一个双?

也许这两件事有一些空间可以解决一些小错误。




Ole Nielsby写道:


首先,对不起,如果这是偏离主题,不是严格意义上的C ++问题。

我找不到ng关于数字或序列化,我认为

这是我最接近的。


现在问题:


我想用人类可读的十进制格式序列化双打

并且当我读回来时确保我得到完全相同的二进制值

。 (现在,我不在乎NaN,无穷大等等)


在本质上,这归结为将双倍转换为

(大)整数尾数和小数指数,然后返回,

这样3.1416将表示为{31416,-4}。


我写了一个转换器总是单独计算比例

并且在阅读

并使用最多17位数的mantiassa写作时完全相同,这应该是

足够精确。然后我用脏测试它。数字

由三角函数和exp函数生成,并且发现

它似乎适用于+/- 20

<范围内的指数blockquote class =post_quotes>
大约,但超出该范围,只有几个百分点

数字不同。


任何人都知道

工作的算法吗?


我尝试了一种算法,当转换双重>文本时,

会转换回双倍并尝试调整尾数

如果这个双倍与原来的不同,但这只会使事情变得更糟。


问候/ Ole Nielsby



你的意思是在同一个平台上?你在同一个应用程序中读取和写入

吗?

如果是这样,那么它可能是可能的,尽管你需要完全支出

数量的精确度。以二进制保存会更好。


如果你想在不同平台之间传输数据,那么它可能无法实现
。这两套硬件可能无法完全代表一组完全相同的双打,你将不得不忍受
而且精度有所下降。无论如何,你应该为

所有的双重计算做好准备,''确切''浮点数是

不是一个非常有意义的概念。


First, sorry if this is off-topic, not strictly being a C++ issue.
I could not find a ng on numerics or serialization and I figure
this ng is the closest I can get.

Now the question:

I want to serialize doubles in human-readable decimal form
and be sure I get the exact same binary values when I read
them back. (Right now, I don''t care about NaN, infinities etc.)

In essense, this boils down to converting a double to a
(large) integer mantissa and a decimal exponent, and back,
so that 3.1416 would be represented as {31416, -4}.

I wrote a converter that always calculates the scaling
separately and does it exactly the same way when reading
and writing, using up to 17-digit mantiassa which should
be sufficient precision. I then tested it with "dirty" numbers
generated by trigonometry and exp functions, and found
that it seems to work OK for exponents in the range +/-20
approximately, but outside of that range, a few percent of
the numbers come out different.

Does anybody know of an algorithm that is known to
work?

I tried an algorithm that, when converting double->text,
would convert back to double and try to adjust the mantissa
if this double was different from the original, but this only
made things worse.

Regards/Ole Nielsby

解决方案

Ole Nielsby wrote:

First, sorry if this is off-topic, not strictly being a C++ issue.
I could not find a ng on numerics or serialization and I figure
this ng is the closest I can get.

It''s good enough. Every language will have a solution, I am guessing,
but it would be language-specific.

Now the question:

I want to serialize doubles in human-readable decimal form
and be sure I get the exact same binary values when I read
them back. (Right now, I don''t care about NaN, infinities etc.)

Output more digits than the precision of the ''double''. See the
''std::numeric_limits'' template.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask



Ole Nielsby wrote:

I wrote a converter that always calculates the scaling
separately and does it exactly the same way when reading
and writing, using up to 17-digit mantiassa which should
be sufficient precision. I then tested it with "dirty" numbers
generated by trigonometry and exp functions, and found
that it seems to work OK for exponents in the range +/-20
approximately, but outside of that range, a few percent of
the numbers come out different.

How are you comparing results?

How are you doing the conversion from two ints into a double?

Maybe there is room in those two things for some minor errors.



Ole Nielsby wrote:

First, sorry if this is off-topic, not strictly being a C++ issue.
I could not find a ng on numerics or serialization and I figure
this ng is the closest I can get.

Now the question:

I want to serialize doubles in human-readable decimal form
and be sure I get the exact same binary values when I read
them back. (Right now, I don''t care about NaN, infinities etc.)

In essense, this boils down to converting a double to a
(large) integer mantissa and a decimal exponent, and back,
so that 3.1416 would be represented as {31416, -4}.

I wrote a converter that always calculates the scaling
separately and does it exactly the same way when reading
and writing, using up to 17-digit mantiassa which should
be sufficient precision. I then tested it with "dirty" numbers
generated by trigonometry and exp functions, and found
that it seems to work OK for exponents in the range +/-20

approximately, but outside of that range, a few percent of
the numbers come out different.

Does anybody know of an algorithm that is known to
work?

I tried an algorithm that, when converting double->text,
would convert back to double and try to adjust the mantissa
if this double was different from the original, but this only
made things worse.

Regards/Ole Nielsby

Do you mean on the same platform? Are you reading and writing from the
same application?
If so then it probably is possible, though you would need to outpout
the full precision for the number. Saving in binary would be better.

If you want to transfer the data between different platforms then it
may not be possible. The two sets of hardware may not be able to
exactly represent an identical set of doubles, you will have to live
with some loss of accuracy. Anyway you should be prepared for this in
all your double calculations, ''exact'' with floating point numbers is
not a very meaningful concept.


这篇关于双 - &gt;文字 - &gt;双的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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