0

65 歳以下の年齢を入力すると、いつでも 2,100,000 未満の数字が得られます。しかし、68 歳以上を入力すると、結果はすぐに uint64_t の最大値である 18,446,744,071,590,568,320 になります。なぜこの結果が急上昇したのか、私にはわかりません。210万くらいまでは問題なく動いています。

// How many seconds have I lived?
#include <stdio.h>
#include <string>
#include <cstdint>
using namespace std;
string addCommas(uint64_t answer);
int main ()
{
    int seconds = 60, minutes = 60, hours = 24, days = 365;
    int years; uint64_t secondsLived, secondsAwake;
    printf("How many years have you lived? ");
    scanf("%d",&years);
    secondsLived = seconds*minutes*hours*days*years;
    secondsAwake = (float)secondsLived*0.666;

    printf("\nYou have existed for %s seconds\n",addCommas(secondsLived).c_str());
    printf("You have been awake for %s seconds\n",addCommas(secondsAwake).c_str());
}
string addCommas(uint64_t answer){
    string num = to_string(answer);
    int insertplace = (int)num.length() - 3;
    while (insertplace > 0) {
        num.insert(insertplace, ",");
        insertplace-=3;
    }
    return num;
}

ここにいくつかの出力があります:

How many years have you lived? 67

You have existed for 2,112,912,000 seconds
You have been awake for 1,407,199,392 seconds


How many years have you lived? 69

You have existed for 18,446,744,071,590,568,320 seconds
You have been awake for 12,285,531,553,090,562,048 seconds
4

3 に答える 3