简体   繁体   English

我应该使用什么数据类型来存储非终止有理数

[英]What datatype should i use to store non terminating rational numbers

For example, when I store 5/3 as a double, I get 1.0 returned. 例如,当我将5/3存储为双精度时,返回1.0。 What datatype should I use for real numbers like 5/3 so that it returns 1.67 in java? 我应该对5/3这样的实数使用哪种数据类型,以便它在Java中返回1.67?

Say 5.0/3 and you will get 1.666....6667. 5.0/3 ,您将获得1.666 .... 6667。

5/3 is integer, truncated to 1. 5/3是整数,截断为1。

Do something like ((double) 5) / 3. This will tell the compiler to cast the values to double. 做类似((double)5)/ 3的操作。这将告诉编译器将值转换为double。

As for what representation one should use with fractions : Either choose a sufficient precision with a double, or create your own fractional type. 至于分数应使用的表示形式:选择具有双精度的足够精度,或者创建自己的分数类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM