问题标签 [rational-numbers]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
897 浏览

java - Rational to double 和 GCF

这个问题是针对最后一种方法的。每当我在方法“public static Rational convert(double d)”中返回 d 时,它会说它不能从 double 转换为 Rational。我将如何做到这一点,以便我能够从方法中返回一个有理数,而无需将“有理”切换为双精度数或参数?那是我如何实现一个循环来在方法中找到 GCF,而不创建另一个方法吗?

0 投票
1 回答
1559 浏览

python - 使用整数 Python 重载运算符

我正在编写一个程序来划分有理数,但我希望它能够处理分数。我想将 1 除以 1/3,但我的程序在处理整数时遇到错误。我试图通过几种不同的方式将整数转换为有理数,但没有任何效果。任何帮助或指导将不胜感激。

这是我不断收到的错误,它是代码底部断言语句中的标志。

回溯(最后一次调用):文件“E:\Python\Rational number extension excercise.py”,第 47 行,在断言 Rational(3) == 1 / r3,“除法测试失败。” TypeError:不支持的操作数类型/:'int'和'Rational'

0 投票
1 回答
1338 浏览

c++ - C++: Why won't erase() remove the first character in my string?

I'm writing a rational class, where the following must be acceptable forms of input:

3/4, 1.4545, (7/8, (7/8), 7/8), or even ((((((5/4))), where my code should rewrite it as 5/4 for the constructor to accept.

At this point I am passing a string parameter into a constructor which calls on formatStringNumerator() and formatStringDenominator() respectively;

I haven't gotten to the decimals yet, but so far, inputs with a parenthesis to the left set the numerator value to 0 somehow. I believe the fault to be the "(" before the numbers. I've commented out my attempt at removing the parentheses to the left, but they just won't go away. I've attempted to use substr() as an alternative, but just got errors when I did. Also, when I print out the find() value I get some large number, [18446744073709551615] when the parameters cannot be found? I was under the impression it should return -1(false?) if not found.

At the first comment, the erase() ought to be deleting the first char in the string, being the parenthesis, but it just does not delete itself, and the conversion from string to int gets messy, defaulting it to 0 like I mentioned before?

0 投票
1 回答
161 浏览

java - 克隆方法 Java

我正在尝试创建一个克隆方法,该方法将采用已经创建的 Rational 并制作一个副本,以便在我的 main 中我可以执行以下操作:

到目前为止,我在我的 Rational 类中创建了一个将有理数作为参数的方法;但是我不确定如何使新实例看起来像参数。

}

非常感谢任何帮助或见解!

0 投票
1 回答
1757 浏览

math - 如何在 Clojure 中将任何数字转换为 clojure.lang.Ratio 类型?

在方案中我可以做到:

在 Clojure 中,我可以做类似的事情:

但在方案中我可以做到:

这在 Clojure 中是不可能的:

如何将 Double(或实际上任何类型的数字)转换为 clojure.lang.Ratio?

在 Scheme 中,我们inexact->exact在 Clojure 中也有类似“double to ratio”之类的东西,但我找不到类似的东西。

0 投票
2 回答
5511 浏览

algorithm - 系列的第 N 项:0,1,3,6,10,15,21,

0,1,3,6,10,15,21,... 每个术语都按自然数的顺序递增我试图生成系列的第 n 个但以 TLE 结尾这是我的代码

任何人都可以帮助我更好的算法。

0 投票
1 回答
102 浏览

ruby-on-rails - Ruby on Rails 中设计的突然合理结果

在 IRB 中进行划分时:

结果始终为 0。

但是在运行在 WEBrick 或 Apache/Passenger 中的实时 Rails 应用程序中,相同的除法会产生1/600type 的值Rational

在 RubyMine 6.3.3 的断点监视中进行除法时,我得到了相同的 Rational 结果

两个参数总是整数。

0 投票
0 回答
103 浏览

floating-point - 在 Boost.Units 中基于自定义单位的数量之间强制执行基于理性的转换

我定义了一个自定义单位系统,它源自 boost::units::si::time。子单位使用 boost::units::make_scaled_unit 定义,因此转换因子使用 boost::units::scale 和 boost::units::static_rational 指定,例如 boost::units::scale<1020, boost::单位::static_rational<1> >。

一般来说,如果不是因为 boost::units::scale::value_type 是“double”,我不会抱怨。因此,数量之间的所有运行时转换都涉及浮点运算,而我的自定义单位之间的比率都是整数,例如 1 个苹果等于 18 个橙子,使用基于有理的因子进行转换就足够了。

有没有办法用其他东西覆盖用于单位转换的浮点算术?谢谢。

编辑:1.我的数量是使用整数类型定义的。

0 投票
2 回答
58 浏览

ruby - 推有理数

我必须创建 class NumberSet,它是不同类型数字的容器,只能包含其中不存在的数字。

此代码截断有理数。例如,(22/7)不应该等于(3/1)

我怎样才能解决这个问题?

0 投票
1 回答
1414 浏览

c++ - 比较有理数

我用所有通用算术函数(+、、、、-和)制作了以下有理数 C++类。*/==!=

使用浮点算术实现剩余的关系运算符运算符 ( <, >, <=, ) 是否可行,还是会导致容易出错的结果?>=

请注意,我只考虑了浮点数,因为在许多情况下交叉乘法会导致整数溢出。