How to design an algorithm that multiplies two floats without '*'? [closed] - c++

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
How do I design an algorithm that takes two floats and multiplies them using only addition, bit shifting and bitwise operations?
I have already found one like this for integers, but that doesn't work with floats.
I have also found another that is much like what I need but log is also prohibited in my case.
The floats are stored according to the IEEE754 standard. I have also tried to keep their exponent part, and bitwise multiply their fractional part with no luck.

According to http://en.wikipedia.org/wiki/IEEE_floating_point, an IEEE754 number x = (-1)^s * c * b^q is represented by s,c,b,q , all are integers. for Two floating point numbers with the same base b is the same.
So the multiplication of two floating point numbers x and y is:
(-1)^(s1+s2)*c1*c2*b^(q1+q2) so the new floating point is represented by: s1+s2, c1*c2, b q1+q2 so you only have left to deal with multiplication of c1 and c2, both are integers so you are done.

Related

How to avoid to show a float: -0.0 [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to show my float number with just 1 digit and avoid to show this case -0.0.
My program is a C/C++ development for an Arduino board.
How can I do that?
(Applies to both C and C++).
IEEE754 floating point defines a signed zero. This is the effect you're observing here. One may obtain negative zero as the result of certain computations, for instance as the result of arithmetic underflow on a negative number, or −1.0 * 0.0, or simply as −0.0.
-0.0 is defined to equal 0.0.
One solution would be to analyse it as a special case: x == -0.0 ? /*handle -0.0 and 0.0 here: e.g. ::fabs(x)*/ : /*non-zero cases here*/.
See http://en.wikipedia.org/wiki/Signed_zero
you can try printing out this way using the c ternary operator.
printf("%2.1f",(f<0 &&f >-1)?-f:f);
here f is an floating point variable and if u get value of f as -0.0001 , the function will print out as 0.0 .
To avoid showing the negative sign you will need to actually round of the number before displaying it since the +/- of a number is irrespective of the precision

get real part from fftw_complex in FFTW [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
this is maybe newbei question , but i dont know how to fast acces to real part of fftw_complex with FFFTW, i cant use .real() method,
I need convert this to double array, dynamic array in c++;
From the docs, 2 second Google search:
4.1.1 Complex numbers
The default FFTW interface uses double precision for all floating-point numbers, and defines a fftw_complex type to hold complex numbers as:
typedef double fftw_complex[2];
Here, the [0] element holds the real part and the 1 element holds the imaginary part.

Calculating large numbers in C++ without external libraries [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I need to write a program that will perform operations on float numbers higher than 10^100.
I can't use any arbitrary precision mathematics libraries that are not included in GCC package by default.
I have NO idea how how to go about it.
Can you point me in the right direction?
You can create a class that can store larger numbers. 12345678 equals to 1234 * 10e4 + 5678.
For large numbers I use string buffers and do manual computation on it. It is much overhead and slow but you get infinite precision.

Bitwise operations vs. logical operations in C++ [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
In C++ are the following statements when comparing two integer values for equality the same? If not, why?
if(a == b)
...do
if(!(a ^ b))
...do
For integer values, yes. Obviously the xor operator will return not-zero if there are any bit differences between A and B, and ! will invert that. For integer data types, the conditions are equivalent.
For floating point values, because of how you can perform two mathematical operations that "should" give the same result, but they may be represented differently as floats, you should not use either of these to compare floats for equality, you should check whether they are the same to within a small margin of error (an "epsilon").
For pointers...I have no idea why you would want to do this to pointers. But if you really want to do it, then yes, they are the same.
However, there is no reason to do this. With optimizations enabled, they will compile to the same code, without, the first will likely be faster. Why would you use the less-clear !(a^b)?
The two comparisons are equivalent: a^b is 0 if and only if a==b, so !(a^b) is true if and only if a and b have the same value.
Whether you can call them "the same" depends on what you mean by two different operations being "same." They probably will not be compiled into the same code, and a==b is definitely easier to read.

Double precision error in C++ [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
I am using openCV C++ libraries and calculated a double. It does the arithmetic but when I read out the number, prints out -1.#QNAN on the command prompt. What does that mean?
I am using a 64-bit i3 processor.
It means you got a quiet NAN, probably by dividing -Inf / Inf or multiplying something with -Inf, or perhaps casing a non-double into a double. It's not so much a precision error as much as it's an arithmetic exception.
EDIT: or adding/substracting Inf ... read more on NaNs here
That's not an error, read more about floating point here