About the double type

Archive of the old editing forum
Forum rules
Before asking on how to use a ZDoom feature, read the ZDoom wiki first. This forum is archived - please use this set of forums to ask new questions.

About the double type

Postby Nash » Thu Mar 16, 2017 9:52 pm

Code: Select allExpand view


double a 
= 42.f;

double b = 360. / 2;



1) When to use or not use the period after the number, if there is no fractional part?
2) What does it even mean?
3) When are you supposed to use f and not use f?
User avatar
Nash
 
 
 
Joined: 27 Oct 2003
Location: Kuala Lumpur, Malaysia
Github ID: nashmuhandes

Re: About the double type

Postby dpJudas » Fri Mar 17, 2017 2:01 am

Not sure if this is the correct answer for ZScript, but for C++ the .f is shorthand for float, while just the dot is shorthand for double.

If you are storing something in a double it is a bad idea to use .f because that may make the calculations a little less accurate. In your example, though, it makes no difference because a float can fully hold the number 42 without any precision loss.

Generally for ZScript I would always use doubles, unless you have a very specific reason to use floats.
dpJudas
 
 
 
Joined: 28 May 2016

Re: About the double type

Postby Graf Zahl » Fri Mar 17, 2017 2:53 am

The '.f' part really has no meaning in ZScript because even if present, it's still using strtod to convert the number into a double. And if you declare a local variable 'float' it will also be made a double implicitly. Single precision floats can only be used as struct, class or array members.
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
 
Joined: 19 Jul 2003
Location: Germany

Re: About the double type

Postby dpJudas » Fri Mar 17, 2017 10:52 am

Nash wrote:1) When to use or not use the period after the number, if there is no fractional part?

Forgot to answer that one. When you add the period that number becomes a double, while if you leave it out the number is an integer. Here's some examples:

Code: Select allExpand view
int a = 5 / 2; // result is 2, because its an integer division
double b = 5 / 2; // result is still 2, because it is an integer division, then converted to a double
double c = 5. / 2; // result is 2.5, because a double divided by an integer is a floating point division
double d = 5 / 2.; // result is 2.5, same reason
double e = 5 / 2 + 5. / 2; // result is 4.5 - first part is an integer division, second part is a float

Which to use: depends if you want to calculate with integer or floating point arithmetics.
dpJudas
 
 
 
Joined: 28 May 2016

Re: About the double type

Postby Nash » Fri Mar 17, 2017 10:59 am

Thanks! So when I want to specifically make the result have floating point precision, I must enforce it by adding the period. :D
User avatar
Nash
 
 
 
Joined: 27 Oct 2003
Location: Kuala Lumpur, Malaysia
Github ID: nashmuhandes


Return to Editing (Archive)

Who is online

Users browsing this forum: No registered users and 2 guests