算术

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

问题描述

大家好,

我要写一个算术对象库。它应该包含

类,如ShortInteger(对于32位长),Double和Float为

标准数字类型包装和LongInteger(真的很长,不是

只有64位)和分数(表示高精度)。

所有这些类都可以从抽象类Number继承。


我得到了坚持以下几点。我不确定如何实现基本的

算术运算。结果类型应该是什么? (我不是说

方法返回类型,但是具体的操作结果类型,即1.0 + 1.0 =

< double> 2.0。)


还有一个问题。如果我将代码放入库中,可以扩展它吗?例如,有些人可能想要实施复杂。


任何想法?

谢谢,

Milan Cermak(捷克共和国)

Hi all,
I''m about to write an arithmetics object library. It should contain
classes like ShortInteger (for 32-bit longs), Double and Float as
standard numeric type wrappers and LongInteger (really long numbers, not
only 64-bit) and Fraction (to express high precission).
All these classes may inherit from abstract class Number.

I got stuck on following point. I''m not sure how to implement basic
arithmetic operations. What the result type should be? (I don''t mean
method return type, but specific operation result type, ie. 1.0 + 1.0 =
<double> 2.0.)

And one more question. If I put the code into a library, it be possible
to extend it? For example some one may want to implement Complex.

Any ideas?
Thanks,
Milan Cermak (Czech Republic)

推荐答案




开始已经工作了。


V



Start working already.

V





你想要解决的最终问题是什么?问题是我没有

算术对象库?


如果你想解决数学问题,哪些问题?如果你试图写一个可能的

库解决它们中的任何一个(包括没有

数字的那些),你只会被卡住。


-

Phlip





这个名字并不重要。但是应该有一个整数类,

可以由处理器直接处理,一个用于其他处理。

(b)为什么你认为你需要包装为double和float ?


我想将它们打包成其他对象的集合。 (我的意思是一个

集合包含不同类型的对象。)也许一个

浮点数的包装就足够了。



The name doesn''t matter. But there should be one class for integers that
could be handled by processor directly and one for others.
(b) Why do you think you need wrappers for double and float?
I want to pack them into collections with other objects. (I mean one
collection containing different kinds of objects.) Maybe one wrapper for
floating point number could be enough.


为什么?


Why?




不是吗对人好点?可能是,它将有助于一些实施

问题。此外,客户可以使用数字并不关心它的类型。



Wouldn''t it be nice? May be, it will help with some implementation
issues. Furthermore client can use a Number doesn''t care about it''s type.



每种类型都应该有自己的加法运算符,结果可能是相同的类型。



Each type should have its own addition operator and the result could
be the same type.




不完全正确。 < ShortInteger> MAX_INT + 1!=< ShortInteger>

当然,您可以采用C ++方式并定义某种_promotion_规则,例如ShortInteger可以在添加到Fraction时提升为分数,或者添加到LongInteger时提升。



Not exactly true. <ShortInteger>MAX_INT + 1 != <ShortInteger>
You could, of course, go the C++ way and define
some kind of _promotion_ rules, e.g. ShortInteger can be promoted to
Fraction when added to Fraction, or to LongInteger when added to it.




这是我喜欢的方式。但它不能满足可扩展性问题。

当我引入新类型的数字时,我需要向所有其他类添加新的促销规则

和执行方法。这可能是不可能的。

我想扩展已编译的库。



This is the way I like. But it doesn''t satisfy the extensibility issue.
When I introduce new kind of Number, I need to add new promotion rule
and execution method to all other classes. This may not be possible when
I want to extend an already compiled library.



如果你满足标准库对st / :: complex实例化的
类型的要求,你可以放弃
''复杂''类型,而是使用std :: complex< yourtypehere> ...



If you satisfy the requirements the standard library imposes on the
type for which std::complex is instantiated, you could forego that
''Complex'' type and instead use std::complex<yourtypehere>...




它可能不是复杂但矩阵或其他什么。


我问的原因是我想了3个多月,但没有找到任何方法来满足extesibility问题。有时候我觉得我想要从糟糕的C ++语言中获取太多东西。 :-)


Milan Cermak



It may not be Complex but Matrix or whatever.

The reason I''m asking is that I thought about this for 3+ months, but
not found any way to satisfy extesibility issue. Sometimes I think I
want too much from the poor C++ language. :-)

Milan Cermak


这篇关于算术的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-16 15:59
查看更多