mardi 3 octobre 2023

C++ type conversion do rounding automatically

Environment: g++ (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0

codes:

#include <string>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;

int main(){
    uint64_t value = uint64_t(1693829987930866899) / 1000.0;
    std::string str_value = std::to_string(value);
    printf("value=%lu, str_value=%s len=%lu\n", value, str_value.c_str(), str_value.size());
}

output: value=1693829987930867, str_value=1693829987930867 len=16

The expected output is 1693829987930866 but I got 1693829987930867.Does the compiler do rounding automaticlly?

Aucun commentaire:

Enregistrer un commentaire