C++计算基于赔率的游戏的支出

C++ calculating the payout on an odds-based game

本文关键字:游戏 于赔率 计算 C++      更新时间:2023-10-16

在过去的几周里,我一直在尝试将双精度(例如 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;
}