| View previous topic :: View next topic |
| Author |
Message |
gogodr I post too much
Reputation: 125
Joined: 19 Dec 2006 Posts: 2041
|
Posted: Tue Jul 21, 2009 1:54 pm Post subject: [C#] 1.0f ? |
|
|
I'm learning through manuals and tutorials some game programming and there is just this thing I don't really understand..
why is there an f when wanting to input some numerical values?
example::
This is a particle system derived from a particle system class I coded with the help of a tutorial::
| Code: |
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace XNa_game_2D
{
namespace particles
{
class ExplosionSmokeParticleSystem:ParticleSystem
{
public ExplosionSmokeParticleSystem(Game1 game, int howmanyeffects, string textureFileName)
: base(game, howmanyeffects, textureFileName)
{
}
protected override void InitializeConstants()
{
MinInitialSpeed = 20;
MaxInitialSpeed = 200;
MinAcceleration = -10;
MaxAcceleration = -50;
MinLifeTime = 1.0f;
MaxLifeTime = 2.5f;
MinScale = 1.0f;
MaxScale = 2.0f;
MinNumParticles = 10;
MaxNumParticles = 20;
MinRotationSpeed = -MathHelper.PiOver4;
MaxRotationSpeed = MathHelper.PiOver4;
spriteBlendMode = SpriteBlendMode.AlphaBlend;
DrawOrder = AlphaBlendDrawOrder;
}
}
}
}
|
MinLifeTime = 1.0f;
MaxLifeTime = 2.5f;
MinScale = 1.0f;
MaxScale = 2.0f;
why is the f needed there?
variables are float declaser in the particle system class file.
|
|
| Back to top |
|
 |
mStorm Expert Cheater
Reputation: 0
Joined: 21 Feb 2009 Posts: 107
|
Posted: Tue Jul 21, 2009 2:08 pm Post subject: |
|
|
| It kind of typecasts your static input.
|
|
| Back to top |
|
 |
BanMe Master Cheater
Reputation: 0
Joined: 29 Nov 2005 Posts: 375 Location: Farmington NH, USA
|
Posted: Tue Jul 21, 2009 2:12 pm Post subject: |
|
|
its a the Float typecast to be more specific..they need to be there so the compiler does not automaticly assume int and compile instructions not used for dealing with floats..
regards BanMe
_________________
don't +rep me..i do not wish to have "status" or "recognition" from you or anyone.. thank you. |
|
| Back to top |
|
 |
gogodr I post too much
Reputation: 125
Joined: 19 Dec 2006 Posts: 2041
|
Posted: Tue Jul 21, 2009 3:19 pm Post subject: |
|
|
so.. basically is to maintain the float propriety of the variable?
o.o? I mean.. is it rally necesary because the variables are already declared as float, I didn't know that they could change their behavior after being declared.
|
|
| Back to top |
|
 |
mStorm Expert Cheater
Reputation: 0
Joined: 21 Feb 2009 Posts: 107
|
Posted: Tue Jul 21, 2009 3:23 pm Post subject: |
|
|
Seems redundant, I know.
So like 25.f
is basically like saying: (float) 25
I guess when the compiler sees "25" it assumes integer, so the 25.f clears things up for it.
|
|
| Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Tue Jul 21, 2009 4:09 pm Post subject: |
|
|
something like
230123.51293812
will be assumed to be a double by default.
(float)230123.51293812 or 230123.51293812f says otherwise (and you will get an error about the implicit conversion failing in C#, C/C++ throw a warning I believe.)
|
|
| Back to top |
|
 |
gogodr I post too much
Reputation: 125
Joined: 19 Dec 2006 Posts: 2041
|
Posted: Tue Jul 21, 2009 11:08 pm Post subject: |
|
|
yeah but like this
float cake;
// insert some code here
public void Chocolate(){
cake = 5.0f;
}
wouldn't the f be like just redundant ?
|
|
| Back to top |
|
 |
nwongfeiying Grandmaster Cheater
Reputation: 2
Joined: 25 Jun 2007 Posts: 695
|
Posted: Tue Jul 21, 2009 11:28 pm Post subject: |
|
|
| gogodr wrote: | yeah but like this
float cake;
// insert some code here
public void Chocolate(){
cake = 5.0f;
}
wouldn't the f be like just redundant ? |
Precision = redundancy. If you want your application to be accurate, it must be redundant.
|
|
| Back to top |
|
 |
gogodr I post too much
Reputation: 125
Joined: 19 Dec 2006 Posts: 2041
|
Posted: Tue Jul 21, 2009 11:39 pm Post subject: |
|
|
| nwongfeiying wrote: | | gogodr wrote: | yeah but like this
float cake;
// insert some code here
public void Chocolate(){
cake = 5.0f;
}
wouldn't the f be like just redundant ? |
Precision = redundancy. If you want your application to be accurate, it must be redundant. | oh well xP its ok then
well.. my main question was "what was that f?" thanks for the help guys, now I know it was for (float) n.n
|
|
| Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Wed Jul 22, 2009 12:07 am Post subject: |
|
|
| gogodr wrote: | yeah but like this
float cake;
// insert some code here
public void Chocolate(){
cake = 5.0f;
}
wouldn't the f be like just redundant ? |
no, because without the f, you would be trying to assign a double (the 5.0) to a float (cake)
| Code: | float f = 200.25f;
f = 501.61661; |
will throw:
error CS0664: Literal of type double cannot be implicitly converted to type 'float'; use an 'F' suffix to create a literal of this type
|
|
| Back to top |
|
 |
gogodr I post too much
Reputation: 125
Joined: 19 Dec 2006 Posts: 2041
|
Posted: Wed Jul 22, 2009 12:20 am Post subject: |
|
|
| ok ok so basically its a help for the engine to process the number as a float since it will try to process it as a double or an integer depends of the number no matter what
|
|
| Back to top |
|
 |
jdm Advanced Cheater
Reputation: 0
Joined: 19 Sep 2008 Posts: 88
|
Posted: Wed Jul 22, 2009 12:56 am Post subject: |
|
|
| gogodr wrote: | | ok ok so basically its a help for the engine to process the number as a float since it will try to process it as a double or an integer depends of the number no matter what |
In a way yes. The compiler automatically assumes all numbers that have a decimal point and a value after are Double Value Types. You may also have to do this with decimals where the suffix would be M. One might think why not use D. Well D is used for double as I think they thought up the value type decimal at a later date when initially createing/designing the language.
_________________
|
|
| Back to top |
|
 |
|