C++计算基于赔率的游戏的支出
C++ calculating the payout on an odds-based game
在过去的几周里,我一直在尝试将双精度(例如 2.76)乘以 int 以产生结果,但最终结果总是四舍五入为 int 当然,双精度并不总是足够精确正如我们已经知道的那样。 我不是专家,我以前从来没有诚实地面对精确数学,C++我已经习惯了PHP。 尽管我确信这是过度询问(我已经在这里用谷歌搜索并搜索了,但没有提出任何解决方案 - 也许我没有正确理解它);有人介意伸出援手吗?
示例代码:
#include <stdio.h>
#include <iostream>
#include <cmath>
double decdouble = 2.72;
int multiby = decdouble * 10127;
couut << "Result: " << multiby << endl;
现在,如果我只想输出到 stdout,我想没问题,我可以使用 setprecision(),但我需要在代码的其他区域中引用它,我不确定我是否能够指定精度保存到变量。
提前非常感谢。
你需要使用双倍而不是 int 进行乘法,以便结果精确,因为 int 返回乘法中最接近 int 的数字。
#include <iostream> /// you don't really need the other libraries you included
using namespace std; /// use this namespace to avoid std:: for each line
int main() /// if you want your code to run, add it in the main() function
{
double decdouble = 2.72;
double multiby = decdouble * 10127; /// use double, not int for a precise result
cout <<"Result: "<< multiby << endl;
return 0;
}
相关文章:
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- c++r值引用应用于函数指针
- 如果编译的源代码是特定于它编译的硬件的,我们如何分发它
- 如何仅使用对象名称打印特定于对象的成员
- 相当于LocaleMatcher的ICU4C
- 等<thing>效于char32_t
- 类似于strcat()的函数出现问题
- 在C++游戏中与库存系统作斗争
- 换位表导致测试失败(但在游戏中运行良好)
- 哪些库可以通过Opencv调整曝光率
- 如何将C++闭包与变量参数同时重用——类似于JavaScript
- Craps游戏问题,忽略if语句
- 算术运算的结果类似于:C浮点变量中的1/3
- 矢量下标超出SFML游戏中的范围
- C++计算基于赔率的游戏的支出
- 从一系列的evens和赔率中,新数组是持续的第一个赔率
- 数据库模块摘要(独立于)游戏的其他模块
- 在C++中使我的游戏独立于图形 API
- 发行控制游戏帧率的c++
- 开发特定于游戏的GUI