MySQL - Floats rounding up/down
#1

Hey guys

Iґve got a small problem with MySQL, I can read and set floats in the DB, but it always is rounding up/down itself.
I couldnt find a fix yet, does someone know how to fix it?

PHP код:
public OnPlayerDisconnect(playeridreason)
{
    new 
Query[256];
    
GetPlayerPos(playeridpInfo[playerid][posX], pInfo[playerid][posY], pInfo[playerid][posZ]);
    
mysql_format(mysqlQuerysizeof(Query), "UPDATE accounts SET Score='%d', VIP='%d', Money='%d', PosX='%f', PosY='%f', PosZ='%f' WHERE ID='%d'",
    
pInfo[playerid][Score], pInfo[playerid][VIP], GetPlayerMoney(playerid), pInfo[playerid][posX], pInfo[playerid][posY], pInfo[playerid][posZ], pInfo[playerid][ID]);
    
printf("%s"Query);
    
mysql_tquery(mysqlQuery"""");
    return 
1;

PHP код:
    pInfo[playerid][posX] = cache_get_field_content_float(0"PosX");
    
pInfo[playerid][posY] = cache_get_field_content_float(0"PosY");
    
pInfo[playerid][posZ] = cache_get_field_content_float(0"PosZ");
    
pInfo[playerid][FacingAng] = cache_get_field_content_float(0"FacingAngel"); 
Reply
#2

Iґve already tried it but it keeps rounding up/down the floats
Reply
#3

That didnt really help
Reply
#4

Floating point values aren't exact because that is their nature. If you really care that much about that 0.001 offset, add '+ 0' to your column in the select query. It's not like you are anyone else is going to notice that they spawn 1 centimeter to the right of where they're supposed to spawn.
Reply
#5

Quote:
Originally Posted by Vince
Посмотреть сообщение
Floating point values aren't exact because that is their nature. If you really care that much about that 0.001 offset, add '+ 0' to your column in the select query. It's not like you are anyone else is going to notice that they spawn 1 centimeter to the right of where they're supposed to spawn.
its not a centimeter its more like when the float is set +0.0003 the spawn position changes for around 8 meters or smthn (kinda weird but its like that, if you believe me or not)

and its not always the same difference its more like its randomly rounding the floats
Reply
#6

Is your enum setup to hold PosX, PosY, PosZ to hold floats?
Also, is your table setup to have these fields store floats as well?

Otherwise I wouldn't know what could be causing this.
A difference of 8 meters or so isn't rounding up/down, it's more like converting from one variabletype to another.
Reply
#7

Quote:
Originally Posted by PowerPC603
Посмотреть сообщение
Is your enum setup to hold PosX, PosY, PosZ to hold floats?
Also, is your table setup to have these fields store floats as well?

Otherwise I wouldn't know what could be causing this.
A difference of 8 meters or so isn't rounding up/down, it's more like converting from one variabletype to another.
The 8 Meters was just used as an example but its some meters away from the spawn where I log out and yes its set to flaot in the DB
Reply
#8

Bump
Reply
#9

Hello Saize,
You have described your problem properly and title will set an exact value to preview if someone searches on ******/bing/etc.

Steps to get your problem fixed
  1. On MySql table, for PosX, PosY, PosZ put type as Decimal
  2. It should be PosX='%.2f' instead of PosX='%f'
I didn't try it, but pretty sure will work fine as my mind saying.
Reply
#10

Iґve already tried it that way

Iґve heard something about mysql_fetch that could fix it, so I checked the wiki but it didnt really help, someone knows another way to fix this?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)