Aller au contenu

Photo

float variables: use the "f" or not use the "f"


  • Veuillez vous connecter pour répondre
8 réponses à ce sujet

#1
Terrorble

Terrorble
  • Members
  • 194 messages
 I thought I better get this clarified.

When I call a function, say DestroyObject(oItem,12.0f);  <-- see the "f"

What's the effect of using the "f" on the end of a float versus not using it in NWN script?

I've seen elsewhere that adding an F limits the size of the number... or something like that.

Gee, I'm really exposing my lack of homework on the matter, but your help is always appreciated.

#2
The Amethyst Dragon

The Amethyst Dragon
  • Members
  • 1 880 messages
I've never really understood it, either. I never use it.

#3
Proleric

Proleric
  • Members
  • 2 354 messages
I no longer use it, either.

#4
Shadooow

Shadooow
  • Members
  • 4 470 messages
I dont think there is any different functionality. I use the faster that is without f.:innocent:

Easy to check imo:

compile a script once with a f and once without, then compare the compiled code (*,ncs) between each other in comparer.

Modifié par ShaDoOoW, 09 février 2014 - 09:57 .


#5
Lightfoot8

Lightfoot8
  • Members
  • 2 535 messages
Have you ever gone from one programming language to another and found that you get a bunch of syntax errors just because you are typing code in the old form. Or converted one language to another .

The 'f' at the end of the float was required in many assemblers/languages. It was left in as optional in many assemblers just for just that reason. It allowed programmers to go from a language that required the 'f' at the end of the float and still write the data type the same way in the current language even though it was not required.

I can remember papers that even suggested that it was best practice to always use the 'f'. It  basically just boils down to convention though. use it if you want to don't use it if you don't. IF you flip back and forth with a language that requires it, you may find it easier to always use it, Then to try and remember when you have to use it.

Modifié par Lightfoot8, 09 février 2014 - 02:05 .


#6
leo_x

leo_x
  • Members
  • 223 messages
I think in this case the guys at Bioware probably cribbed from a C/C++ language lexer/parser, which uses 'f' to distinguish float literals from double literals. Since NWScript has no doubles, there probably is no difference one way or the other when compiled.

Modifié par pope_leo, 09 février 2014 - 07:23 .


#7
Lightfoot8

Lightfoot8
  • Members
  • 2 535 messages

pope_leo wrote...

I think in this case the guys at Bioware probably cribbed from a C/C++ language lexer/parser, which uses 'f' to distinguish float literals from double literals. Since NWScript has no doubles, there probably is no difference one way or the other when compiled.


Yes that is mostlikely the case.  but even in C++  the f is optional.   It is the default case when no letter follows the number.   If you wanted to force the 80 bit float,  the  then the 'l' would be required to follow It.   Since it is not the default case. 

#8
Tarot Redhand

Tarot Redhand
  • Members
  • 2 687 messages
My understanding is that it is there to help the compiler in the case where you want to assign a literal value with no fractional part to a float variable (eg MyFloat = 1f). As such it is redundant as you are unlikely to receive a syntax error if you omit the f.

TR

#9
Terrorble

Terrorble
  • Members
  • 194 messages
Glad to hear it is of nominal importance, since I thought it looked nice one day and used it and can't remember where.

Thanks, everyone.