vector calculations without floats

Hi,

Is there any way to perform vector calculations without using floats? I thought it might be possible by scaling up an int by 1000 times before performing calculations and then just ignoring the last 3 digits of the output but I can't figure out how to calculate a square root with ints.

This page might help,

The second C function seems to be the best


http://www.codecodex.com/wiki/Calculate_an_integer_square_root
Thanks for the reply.

That's great for whole number square roots but what I'm after is something that will use a scaled up int to represent a float, so if the root of a number is 1.234 for example it will be represented as 1234, 1.23 as 1230. and 1.23456789 as 1234. I've been racking my brains over this but I fear it exceeds my abilities

I am a little unclear on why you would want to that, and what that has to do with "vector calculations". A floating point number is already stored in scientific notation. It uses a binary exponent though. If you attempt to put a float into an int, you will have overflow and underflow problems. A float can represent up to 10**38 and a double up to 10**308, but you only get 24 and 53 bits of precision. Sory if you already know that; I am just trying to understand what you are attempting to do.


Im not sure if this is what you mean and where you are scaling up but this would work for
numbers scaled up * 1000.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int intSqrt (int valIn)  
 {  
	int		root;
	float	temp = float(valIn);

	temp /= 1000;

	temp = sqrt(temp);

	temp *= 1000;

	root = int(temp);

     return root;  
 }  


Hope this was Helpful
Shredded
This usage is called fixed point arithmetic. Look that up.
Yes, I understand multiplying by a scaling factor.
What do you mean with vector calculation? why do you need the square root? (maybe you could work with the square numbers instead)
I need to use square roots to calculate the length of a vector and don't know a way of doing it without using floats.

Maybe I shouldn't have put so much emphasis on the vector element, that's just what I personally will be using the square root for, what I need is a way to find the square root of a number without using floating point variables.
Last edited on
Well, you might want to take a look at this wiki article:

http://en.wikipedia.org/wiki/Methods_of_computing_square_roots

It has some code examples. Some of the approximation methods my still function properly using integer operations only. They seem to show a binary algorithm only using integers, but I don't have the time right now to figure out how it works.
I thnk the OP is talking about vector forces which have magnitudes and directions not a vector container.
1. most experts will tell you not to use floats, rather use double precision.
2. if you do not want to use square root use the sine or cosine functions
3. by using setprecision(2) you can set precision to two decimal places.
4. you could also caste your double result to int to eliminate the fractional part.
h.t.h`s
1. most experts will tell you not to use floats, rather use double precision.
They will tell you to use integers. The calculations are a lot fasters, you can use the GPU, and the result is exact.
Also, there are a lot of times when you don't want to calculate the values, just know if something happens. By instance: point inside circle, point inside polygon, segment intersection, point in line, visible face, bigger angle.

In general, there is no reason to go through the extra work required to use integers for something that is best expressed as a floating point value. There is, of course, some special cases.

Floating point calculations are not necessarily slow since most general purpose processors have fully pipelined floating point units (can produce a result on every clock) and also many have superscalar FP capabilities (multiple execution units). Floating point performance depends on what your calculations are like, and if latency is a factor due to dependency chains. Integers have the same dependancy chains, but are often lower latency.

It used to be that floating point was slow, since it was an external unit and/or not pipelined. That is not the case anymore. Similar thing happened with shift operations. Most processors have a hardware shifter that can shift any number of bits in a single instruction.

SIMD units changes the balance also, since you may actually have higher instruction throughput using floating point values since the FP calculations will not be using integer resources, and can execute at the same time as independent integer instructions. I ran into this with just integer instructions, since the lowest number of instructions is no longer an accurate metric of performance. Newer processors can execute anywhere from 2 to 4 integer instructions, out of order, per clock. Memory access tends to be more important now; many instructions are essentially free (will be executed simultaneously with other instructions).

Also, current GPUs use floating point units; they may still have integer units or they may be faking it with FP units. It is no longer 8-bits per color component (32-bit RGB+alpha), it is a 32-bit float for each color component (128-bit color essentially). They are generally much faster for floats than doubles though, if they even support doubles.
Topic archived. No new replies allowed.