Documentation?

Previous topic - Next topic

Cliff3D

Quote from: MrTAToad on 2010-Sep-09
Not quite - the default variable is floating point and the default result is floating point too.

Umm - I'm trying to get a nice clear consistent picture so that any references I make (or copy) in the Wiki are as clear and accurate as possible. This link from the manual:

http://www.glbasic.com/xmlhelp.php?lang=en&id=255&action=view

and specifically quotes like this:

QuoteTo force calculations to be done as a floating point calculation, add a ".0" to the end of any number typed into your code that you wish to be interpreted as a floating point number.

which I was reading as indicating that the default calculation/result type was integer, unless something specifically denoted floating point (like a trailing .0). I'm just getting a slightly confused "vibe" I guess :( hopefully - having worked out tutorial 10/OneMore to my satisfaction, I'll manage some tutorial 1 editing in the Wiki today :)

mahan

#61
I'm a newbie in GLBasic so correct me if I'm wrong but:

Adding .0 after plain numbers/constants in the source, tells the compiler to treat this number/constant as a float variable.

The compiler (if it behaves like most other compilers, which I suppose since it's a precompiler for C(++)) expands expressions to the highest precision and does the calculations from there.

Test this:

Code (glbasic) Select

STDOUT (10/3)
STDOUT "\n"
STDOUT (10.0/3)
STDOUT "\n"
STDOUT (10/3.0)
STDOUT "\n"

KEYWAIT


Hope this somewhat clears the matters up.

EDIT: to run this go to menu Project-Options and check the "Console" checkbox.

MrTAToad

Ah - good examples there...

Cliff3D

Yup, it's showing exatly what I was talking about - GLBasic assumes both constants to be integer and/or performs integer math on them UNLESS there is an explicit "float" instruction (in the form of a decimnal place).

Working on transferring and updating bits of manual to the Wiki, I had come to this phrase:

QuoteWhen you create a variable for the first time (e.g. using LOCAL/GLOBAL command, or at the time of first use) you can append a '%' character to its name to indicate that this variable is an integer number. (An integer is a number without a fraction part). Appending an optional '#' will make the variable a floating point number (one with a fractional part). The appendices ('%' or '#') are optional for further use of the variable.

*Note*
GLBasic uses floating point numbers by default. Thus, if you wish to perform integer only calculations, make sure you use the function INTEGER().

(first paragraph included for context only) which felt... inaccurate. I am trying, in my own way, to try and reduce the inaccuracies in the documentation as I copy over, changing programs and text to reflect the "now" rather than the "then" of when the posts were made (so that programs compile and run, for example). I have already reworded the line to ommit the comment that floating point numbers are used by default and you must force the use of integers as that's not entirely accurate, but have left the info about how to use the INTEGER keyword.


Or, to put it another way - run away, run away!! ;)


mahan

If you omit the % or #, the compiler assumes a float-variable. (this is a difference to many other basics out there btw.)

Another important thing in the context is that, while % or # in subsequent usage (after the declaration is done) is optional, the $-sign after string variables is NOT optional iirc.

This is because $ is expanded by the precompiler to "_Str" in the intermediate C(++)-format and thus using the same name without the $ refers to a different name on that level.

Cliff3D

Quote from: mahan on 2010-Sep-09
If you omit the % or #, the compiler assumes a float-variable. (this is a difference to many other basics out there btw.)

Yup - if that were reversed then it would all feel more logical/consistent to me. Oh, and as a by-product thousands of programs would no longer compile correctly, or would fail during execution, because of the (purely hypothetical!) change. The main thing seems to be "be careful out there" ;)

Cliff3D

#66
Quote from: Ocean on 2010-Sep-09
Quote from: Cliff3D on 2010-Sep-09
"be careful out there" ;)

Hill Street Blues fan by any chance?

I watched it a time or two, back in the day :)

Ian Price

The whole float/integer thing has caught me out a couple of times too. It's odd, but you get used to it. Eventually :P
I came. I saw. I played.