c ++从“ float”到“ int32”的转换,可能会丢失数据

时间:2019-01-12 19:33:02

标签: c++

构建此代码,返回conversion from 'float' to 'int32', possible loss of data

我如何解决它,我求学cpp

uint16 nRandom = Radius;
        if (nRandom <= 0)
            fRandom_X = (float)fX;
        else
            fRandom_X = (float)(myrand((fX - nRandom) * 10, (fX + nRandom) * 10) / 10);

        nRandom = Radius;
        if (nRandom <= 0)
            fRandom_Z = (float)fZ;
        else
            fRandom_Z = (float)(myrand((fZ - nRandom) * 10, (fZ + nRandom) * 10) / 10);

        pNpc->SetPosition(fRandom_X, 0.0f, fRandom_Z);

1 个答案:

答案 0 :(得分:2)

(此回答仅限于IEEE754。)

int32不能存储double可以存储的所有可能值(反之亦然),因此您的编译器会向您发出有用的警告。

经验法则是不要将您的类型混合在一起:使用一种类型并坚持下去。

如果您需要深入研究浮点数,那么为了生活轻松(现在可能是更快的代码),请使用float而不是doubleint32可以存储var OneSignal = window.OneSignal || []; OneSignal.push(function() { OneSignal.init({ appId: "", autoRegister: false, notifyButton: { enable: true, }, allowLocalhostAsSecureOrigin: true, promptOptions: { actionMessage: "We'd like to show you notifications for the latest news and updates.", acceptButtonText: "ALLOW", cancelButtonText: "NO THANKS" }, welcomeNotification: { "title": "My Custom Title", "message": "Thanks for everything!", } }); OneSignal.on('subscriptionChange', function (isSubscribed) { console.log('subscriptionChange'); if (isSubscribed) { OneSignal.sendTags({ user_id: 9123 }).then(function () { console.log('tag:' + 9123); }); } }); if (Notification.permission === "granted") { // Automatically subscribe user if deleted cookies and browser shows "Allow" OneSignal.getUserId() .then(function(userId) { if (!userId) { OneSignal.registerForPushNotifications(); } }) } else { OneSignal.showHttpPrompt(); } }); 可以存储的每个值,因此警告将消失。