alliedmodders / sourcemod

SourceMod - Source Engine Scripting and Administration
http://www.sourcemod.net/
991 stars 428 forks source link

ConVar.IntValue returns the wrong value #1800

Open ecsr opened 2 years ago

ecsr commented 2 years ago

Help us help you

Environment

Description

ConVar.IntValue returns the wrong value for larger odd numbers. I'm only able to reproduce this on Linux; on Windows everything is working as expected. There is an old issue (#648) from 2017 that reported the same problem but for CS:GO.

Problematic Code (or Steps to Reproduce)

public void OnPluginStart()
{
    ConVar convar = CreateConVar("test_intvalue", "35781985");
    PrintToServer("IntValue: %d", convar.IntValue);
}

Logs

game output:

sm plugins load test-intvalue.smx
IntValue: 35781984
[SM] Loaded plugin test-intvalue.smx successfully.
einyux commented 2 years ago

Looks like an issue from Valve.

https://github.com/ValveSoftware/source-sdk-2013/blob/0d8dceea4310fde5706b3ce1c70609d72a38efdf/sp/src/tier1/convar.cpp#L784

The value is converted from string to float, then float to int. The issue is that "35781985" is not exactly representable in floating-point (See https://www.h-schmidt.net/FloatConverter/IEEE754.html), so "35781985" is converted to 35781984.0, then 35781984.

=> Solution: Ask Valve to convert the string to int directly :D