Calculation results differs with data types

Discussions about Coding and Scripting
Post Reply
User avatar
Barbie
Godlike
Posts: 2802
Joined: Fri Sep 25, 2015 9:01 pm
Location: moved without proper hashing

Calculation results differs with data types

Post by Barbie »

I wonder why these calculations give me different results:

Code: Select all

var() config byte RadarPosRatioX; // 0=left, 100=right
...
simulated function DrawRadar(canvas Canvas, ERDR_Position APosition, int RangeRadius) {
local int RadarCycleRadiusOuter;
local int OriginX, OriginY;
local float f;

	RadarCycleRadiusOuter = 123;
	RadarPosRatioX = 100;
	
	OriginX = (Canvas.SizeX - 2 * float(RadarCycleRadiusOuter)) / 100 * float(RadarPosRatioX) + float(RadarCycleRadiusOuter); // (1)
	f       = (Canvas.SizeX - 2 *       RadarCycleRadiusOuter)  / 100 *       RadarPosRatioX +        RadarCycleRadiusOuter;
OriginX is always (significant) smaller or equal to f although every operand of the right side of the assignment (1) is converted to float.
"Multiple exclamation marks," he went on, shaking his head, "are a sure sign of a diseased mind." --Terry Pratchett
Higor
Godlike
Posts: 1866
Joined: Sun Mar 04, 2012 6:47 pm

Re: Calculation results differs with data types

Post by Higor »

Never operate with 'byte', conversion to and from float is mandatory.
Post Reply