Qbasicnews.com

Full Version: bug in FIX() ?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hmmm...
Yesterday I have had problems with FIX() in FB.
I have here in the internetcafé now not the code. But it seems, that it does nothing.
I have seen, that a program in FB 0.10 do something different than QB.

For example
In QB
a# = FIX(b#)
is nearly the same like
a# = INT(b#-0.5)

But in FB it isn't it.
But I can at the moment not say more about it, because I have not the code here, where is error is.

Greatings
theuserbl
In QB, fix() truncates any digits after the decimal point, so 3.142 would become 3, and 4.77 would become 4. I'm pretty sure FreeBASIC does the same thing, but I'll have to check it out.
Actually, FIX is the same as INT, but it handles negative numbers in a different way, FIX(-4.5) will return 4 while for INT(-4.5) result will be 5, positive numbers will be always the same.
Then how the hell have I been using it to truncate numbers!?!? I'm confused, maybe I have a special magical version of QB... mmm... magicy.... Tongue
There is a little difference: in QB

INT(4.5) is 4 (not common, ask Moneo Big Grin )

but in FB INT(4.5) is 5 (as you would expect)
INT() is the same a floor() in C, what will truncate the number (4.99 becomes 4.0).

CINT() will round the number (4.51 becomes 5).
Excuse me, I meant CINT, or the standard typecast from floating point to integer.
In QB 4.5 goes to 4 and 4.500001 goes to 5, this is a "feature" of QB, Moneo has started some of threads about this...

FB is more standard, 4.499999 "cints" to 4 and 4.5 goes to 5

It caused me some problems when porting a small graphics demo...
I got the same result with both, QB 4.5 (compiled) and FB 0.11:

Quote:print cint( 4.5 ), cint( 4.500001 ), cint( 4.499999 )

prints: 4 5 4

FB will set the FPU to do rounding the same way as in QB, no function is invoked when "calling" CINT() in both compilers, so, they can't be different, unless there's something wrong with the FPU ;)
Ok, thanks to Antoni, who got me into this discussion.

First of all, I don't know FB, so I'm not qualified to speak on this.

Regarding QB, you guys seem to use INT, FIX and CINT indistinctly. If you read your manual or the QB Online Help provided in this forum, you will find that they are quite different. Of course, you will find certain numbers that give the same results.

INT - a math function that returns the largest integer less than or
equal to a numeric-expression. Basically, this is a "truncate the decimals" function, except you won't get what you expect with certain negative numbers.

FIX - this is defined as a truncate function, but again, you'll get differences with negative numbers.

CINT - This is defined as a rounding function. It uses a rounding method other that the "conventional" whereby odd numbers ending in .5 round up correctly, but even numbers ending in .5 just truncate. Example: 123.5 gives 124, but 124.5 gives 124.

Some other goodies about CINT is that negative numbers give weird results, and it can't handle numbers greater that 32K.

CINT is a real piece of work, and that's why I never use it, period.

Please, don't confuse rounding with truncating. If you want to round, I suggest you use the following code developed and thoroughly tested for the "conventional" rounding method by Oracle and I:

ROUNDED = SGN(x) * INT(ABS(x) + .5)

where x is the variable (single or double) to be rounded. This will handle negative numbers as you would suspect; that is, rounded to a greater negative number.

My best advise is: when you decide on a rounding or truncating algorithm, please run a test, like put your algo in a FOR loop ranging from negative numbers into positive numbers, incrementing by decimas. An increment of .5 makes a good test. Output the results to a file, and sight-check the results.

Gee, this turned into a tutorial. :wink:
*****