🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Commonly Used Forms of Calculus

Started by
9 comments, last by Otays 5 years, 8 months ago

What types of calculus are used most commonly in programming? Is there a need for it unless working with 3D graphics?

Advertisement

It really depends what you're doing. For simple gameplay programming, business apps, etc. you may not need any at all.  Writing your own physics simulation may need quite a lot of (or be so simpler/better with) calculus.

- Jason Astle-Adams

5 hours ago, RidiculousName said:

Is there a need for it unless working with 3D graphics?

Really it come to games (both 3D and 2D) in simplified form from a CAD-related and scientific-related software. Generally a differential, integral and tensor calculus used into any branch of phisics, so it impossible to build any real-world production-related software (CAD/CAM/Scienific and so on) without calculus.

#define if(a) if((a) && rand()%100)

12 minutes ago, jbadams said:

For simple gameplay programming, business apps, etc. you may not need any at all. 

Really for ever simple gameplay adding a velocity vector to position is a integration of differential equation of movement using a simpliest possible finite-difference scheme. So it works by common rules of finite-difference integration numerical stability conditions, and can use a common extensions of schemes and so on.  For example most of people here know that for accelerated movement averaging of velocity vector on start and end of  time-frame significatly improve numerical stability, but how many people know why it happend and how to extend it to case with variable acceleration?

#define if(a) if((a) && rand()%100)

There are many things in calculus that are used in game programming:

  • derivatives (e.g. many optimization problems, computing normals to a surface),
  • computing integrals (e.g., the rendering equation),
  • integrating differential equations (e.g., Physics simulations, or gameplay where velocity is added to a position, as Fulcrum.013 points out).

[Note on Fulcrum.013's post: In games you rarely care about the accuracy of an integrator that much: Stability (implicit methods) or preservation of energy (symplectic integrators) are often much more important features.]

You also get to use calculus in programming in general. If you have an algorithm with a loop that takes time proportional to 1/k for k=1..n, the whole thing will take time roughly proportional to log(n), because the sum 1/1+1/2+...+1/n can be approximated with the integral of 1/x from x=1 to x=n.

There are more exotic parts of calculus that also get used. For instance, spherical harmonics can be used in global illumination computations.

You should learn calculus because it's important to understand the world. As a bonus, you get to use it in programming games! :)

 

fast inverse square root is a common engine function used for calculating surface normals for lighting, and it combines the magic of bit level hacking with some root finding calculus tricks. 

this involves calculating the first few terms of a Taylor series.  Taylor series are pretty cool in general.

 

https://en.wikipedia.org/wiki/Fast_inverse_square_root


float Q_rsqrt( float number )
{
	long i;
	float x2, y;
	const float threehalfs = 1.5F;

	x2 = number * 0.5F;
	y  = number;
	i  = * ( long * ) &y;                       // evil floating point bit level hacking
	i  = 0x5f3759df - ( i >> 1 );               // what the #*@!? 
	y  = * ( float * ) &i;
	y  = y * ( threehalfs - ( x2 * y * y ) );   // 1st iteration
//	y  = y * ( threehalfs - ( x2 * y * y ) );   // 2nd iteration, this can be removed

	return y;
}

 

 

If you count polymorphic lambda calculus as calculus, the entire foundation of object oriented programming that saved the world of computer science from the software crisis in the 1970s is in a sense a "calculus"

https://en.wikipedia.org/wiki/System_F

"... simply typed lambda calculus has variables ranging over functions, and binders for them, System F additionally has variables ranging over types, and binders for them. "

 

 

Amber resin drips from the roof of the house.

???? ???????? ?????

1 hour ago, Otay said:

fast inverse square root is a common engine function

Wouldn't this be a lot faster these days?  That one you quote is from the early 90s.


	float FastInvSqrt( const float number )
	{
		return _mm_cvtss_f32( _mm_rsqrt_ss( _mm_set_ps1( number ) ) );
	}

 

"Those who would give up essential liberty to purchase a little temporary safety deserve neither liberty nor safety." --Benjamin Franklin

Just now, CrazyCdn said:

Wouldn't this be a lot faster these days?  That one you quote is from the early 90s.



	float FastInvSqrt( const float number )
	{
		return _mm_cvtss_f32( _mm_rsqrt_ss( _mm_set_ps1( number ) ) );
	}

 

I dono ive never seen this one, the one i posted was 4x faster than the regular computation. 

How fast is this method?

Amber resin drips from the roof of the house.

???? ???????? ?????

1 hour ago, Otay said:

I dono ive never seen this one, the one i posted was 4x faster than the regular computation. 

How fast is this method?

It may have been faster 20 years ago, but I had read it was slower then using SSE, I didn't test the above, came from an SO Q/A from a few years ago and I took the test data at face value.  I was more pointing out that the link you posted is from tech from like 1991, things have changed quite a bit since then :)

"Those who would give up essential liberty to purchase a little temporary safety deserve neither liberty nor safety." --Benjamin Franklin

11 minutes ago, CrazyCdn said:

It may have been faster 20 years ago, but I had read it was slower then using SSE, I didn't test the above, came from an SO Q/A from a few years ago and I took the test data at face value.  I was more pointing out that the link you posted is from tech from like 1991, things have changed quite a bit since then :)

 

Ah yeah, a quick google search confirms its been obsolete since 1999, and there's a big article on it here:

http://assemblyrequired.crashworks.org/timing-square-root/

In response to the original question about calculus in programming, I think calculus comes in handy most for analyzing data structures and algorithms (like Alvaro mentioned) more than it does directly in the code.  Don Knuth, the "father of algorithm analysis" wrote several volumes on the art of computer programming, and flipping to a random page usually turns up an integral or two.

There's a lot of interesting math stuff that comes up unexpectedly in computer science, like how collapsing sets of imaginary numbers are what make fast fourier transforms "fast"

Amber resin drips from the roof of the house.

???? ???????? ?????

This topic is closed to new replies.

Advertisement