error 20: Type mismatch with integer pointer

General FreeBASIC programming questions.
Post Reply
badidea
Posts: 2594
Joined: May 24, 2007 22:10
Location: The Netherlands

error 20: Type mismatch with integer pointer

Post by badidea »

Hello all, it is either getting too late to think clear, or the compiler is acting weird today. Why do I get this:
error 20: Type mismatch in 'pInt = @i + int(20 * 0.5)'

Code: Select all

dim as integer i
dim as integer ptr pInt

pInt = @i + 10

pInt = @i + int(20 * 0.5)
edit: I figured it out. int <> integer:

Code: Select all

pInt = @i + cast(integer, int(20 * 0.5))
fxm
Moderator
Posts: 12159
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: error 20: Type mismatch with integer pointer

Post by fxm »

As '20 * 0.5' is a Double, 'int(20 * 0.5)' is also a double.
See INT documentation page.
Operations on pointers work only with integral numbers.
badidea
Posts: 2594
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: error 20: Type mismatch with integer pointer

Post by badidea »

That's crazy. I'm out. Back to C :-)
caseih
Posts: 2158
Joined: Feb 26, 2007 5:32

Re: error 20: Type mismatch with integer pointer

Post by caseih »

Indeed that does seem a rather strange choice at first blush. But it's not FB's fault. This is the way QuickBasic and other BASICs have done it for decades. Even more confusing, if you come from other languages, then Int() would almost always be not what you'd want. Fix() would probably be more appropriate. But like Int() it returns the same type it was given. In an ideal world, Int() would be renamed Floor(), and Int() would do what you expect, which is the same as Fix() but always returning an integer.
badidea
Posts: 2594
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: error 20: Type mismatch with integer pointer

Post by badidea »

caseih wrote:Indeed that does seem a rather strange choice at first blush. But it's not FB's fault. This is the way QuickBasic and other BASICs have done it for decades. Even more confusing, if you come from other languages, then Int() would almost always be not what you'd want. Fix() would probably be more appropriate. But like Int() it returns the same type it was given. In an ideal world, Int() would be renamed Floor(), and Int() would do what you expect, which is the same as Fix() but always returning an integer.
I understand the legacy. I started with QBASIC myself. Only today (after almost 25 years!) I learned that int(...) is different then I always thought.
Fix(...) is not right either (https://freebasic.net/wiki/wikka.php?wakka=KeyPgFix):

Code: Select all

declare function Fix ( byval number as single ) as single
declare function Fix ( byval number as double ) as double
declare function Fix ( byval number as integer ) as integer
declare function Fix ( byval number as uinteger ) as uinteger
Only the rounding is different.

cint(...) or cast(integer, ...) seem ok.
FreeBASIC is starting to look like PHP with so many build-in statements.
I really should return to C, much cleaner and consistent :-)
caseih
Posts: 2158
Joined: Feb 26, 2007 5:32

Re: error 20: Type mismatch with integer pointer

Post by caseih »

Sure. My point was Fix() is more what most people want than Int(), at least as far as chopping off the decimal part goes. But yes BASIC has a lot of legacy baggage, like many languages do.

Note that Cint() is probably not what you want either. Cint() actually rounds the number according to an even/odd rounding scheme so 1.5 becomes 2, although 2.5 becomes 2 also. What you want is the result Cint(Fix(foo)).
Post Reply