## Can a IEEE 754 real number “cover” all integers within its range?

No, not all, but there exists a range within which you can represent all integers accurately. Structure of 32bit floating point numbers The 32bit floating point type uses 1 bit for the sign 8 bits for the exponent 23 bits for the fraction (leading 1 implied) Representing numbers Basically, you have a number in the … Read more

## Calculating distance between zip codes in PHP

This is mike’s answer with some annotations for the magic numbers. It seemed to work fine for me for some test data: function calc_distance(\$point1, \$point2) { \$radius = 3958; // Earth’s radius (miles) \$deg_per_rad = 57.29578; // Number of degrees/radian (for conversion) \$distance = (\$radius * pi() * sqrt( (\$point1[‘lat’] – \$point2[‘lat’]) * (\$point1[‘lat’] – … Read more

## Shortest distance between points algorithm

http://en.wikipedia.org/wiki/Closest_pair_of_points The problem can be solved in O(n log n) time using the recursive divide and conquer approach, e.g., as follows: Sort points along the x-coordinate Split the set of points into two equal-sized subsets by a vertical line x = xmid Solve the problem recursively in the left and right subsets. This will give … Read more

## How to compute the nth root of a very big integer

If it’s a REALLY big number. You could use a binary search. def find_invpow(x,n): “””Finds the integer component of the n’th root of x, an integer such that y ** n <= x < (y + 1) ** n. “”” high = 1 while high ** n <= x: high *= 2 low = high/2 … Read more

## get closest point to a line

Here’s Ruby disguised as Pseudo-Code, assuming Point objects each have a x and y field. def GetClosestPoint(A, B, P) a_to_p = [P.x – A.x, P.y – A.y] # Storing vector A->P a_to_b = [B.x – A.x, B.y – A.y] # Storing vector A->B atb2 = a_to_b[0]**2 + a_to_b[1]**2 # **2 means “squared” # Basically finding … Read more

## Compute fast log base 2 ceiling

If you can limit yourself to gcc, there are a set of builtin functions which return the number of leading zero bits and can be used to do what you want with a little work: int __builtin_clz (unsigned int x) int __builtin_clzl (unsigned long) int __builtin_clzll (unsigned long long)

Categories c

## How to calculate Tangent and Binormal?

The relevant input data to your problem are the texture coordinates. Tangent and Binormal are vectors locally parallel to the object’s surface. And in the case of normal mapping they’re describing the local orientation of the normal texture. So you have to calculate the direction (in the model’s space) in which the texturing vectors point. … Read more

## Avoiding problems with JavaScript’s weird decimal calculations

1.2 + 1.1 may be ok but 0.2 + 0.1 may not be ok. This is a problem in virtually every language that is in use today. The problem is that 1/10 cannot be accurately represented as a binary fraction just like 1/3 cannot be represented as a decimal fraction. The workarounds include rounding to … Read more

## Fitting polynomials to data

Thanks for everyone’s replies. Here is another attempt at summarizing them. Pardon if I say too many “obvious” things: I knew nothing about least squares before, so everything was new to me. NOT polynomial interpolation Polynomial interpolation is fitting a polynomial of degree n given n+1 data points, e.g. finding a cubic that passes exactly … Read more

## Rotate a point by another point in 2D

If you rotate point (px, py) around point (ox, oy) by angle theta you’ll get: p’x = cos(theta) * (px-ox) – sin(theta) * (py-oy) + ox p’y = sin(theta) * (px-ox) + cos(theta) * (py-oy) + oy