C/C++ :: Fastest / Most Efficient Way To Add Two Fractions
Jul 12, 2012
I have to write a function
struct rNumber add(rNumber a ,rNumber b);
which adds two rational numbers in following representation :
rNumber := s*(n/d)* 2^e
struct rNumber{
_byte_t s; // sign (do not consider for this question)
uint n; //numerator
uint d;// denominator
short e;//exponent
}
If the exponents of both numbers are not equal, then they have to be made equal in order to add them. This can be made in 4 ways : increase or decrease the n or d of both numbers.
But if we decrease the denominator of a number (a.d =1) by shifting it for example 1 bit to the right, we get 0 which leads to INFINITY for the fraction. In another case decreasing the numerator would lead the n to be 0 which meanse the whole fraction is then 0.
According to this, in worst case, all 4 cases has to be checked for the right result.
So far the UNDERFLOW of n or d is considered. If we try to increase the value of n or d, then OVERFLOW may also occur.
The very first, intuitive solution would be iteratively increase/decrease one of the terms and to check if the change leads to ZERO or INFINITY.
I'm currently building a new data structures that will be used in monte carlo generators (and so will be constructed several million times) and I'm wondering what's the best way (computer-speed-wise) to pass the data to the constructor. I am currently doing it using references and passing arrays as pointers like this:
Code: class particle{ public: particle(double *ar,int &id):IDup(id){ for (int i=0;i<5;++i) Pup[i]=ar[i]; } int IDup; double Pup[5]; };
I'm assuming that since using references has no need to create a temporary memory slot it's more efficient .....
As for the arrays: is there a way for me to pass them as reference as well? (not using c++11), I'm using arrays instead of vectors as much as I can because I assume that vectors, being more advanced data structures, would take more time to create.
I'll process the raw content of a stream which obviously will be loaded one chunk at a time into an buffer.I need to check if every chunk content is x00 filled. If it is, I'll increase the blank chunks counter of 1.On your personal opinion, which is the fastest an less cycles consuming manner to achieve such result?
I was wondering this: is it possible to make an instant XOR of all the buffer content to check if it does return 0 ?the only way is it to cycle through all the bytes and compare each one of them with 0?
Names are std::string no more than 3 characters in length. What built in container or boost container is fastest at finding a name (Key) in it's container.
Finding a name. At the name will be a value double.
I need the most efficient at finding and finding/updating the value. Forget out inserts and deletes.
Suppose you have defined a container of elements and you want do define a comparison function between elements based on the ordering of the elements in that container. What algorithm for the comparison would be the most efficient?
My current idea is to simply iterate from the beginning of the container, and whichever of the two elements is found first is the lesser (assuming the second is not the same as the first). It seems kind of naïve though. Any better performing algorithm? This is what I have so far:
Would perhaps forcing the container to have a random access iterator, like vector, and then writing a specialized comparison function based on that perform even faster? Or perhaps force the container to be a map to integers, and compare the elements by comparing the integer mapped values?
i recently started programming. i mean I've been exposed to programming for the first time about a week ago. I've been following the tutorial here and playing around with my own code. For some reason, this code works while receiving decimals but not fractions.
Code:
#include <stdio.h> int main() { float kd; float kd_2; printf("Please enter your k/d "); scanf("%f", &kd ); }
I'm looking for a library that handles rational, irrational and trascendental numbers and calculates the exact results without approximating values. For example, if I want to calculate:
a = pi; b = 3; c = 2;
I want this library to return the result in this way:
sqrt(b*c)*a == sqrt(6)*pi
instead of
sqrt(b*c)*a == 7.6952989
In case it matters, I'm working on Ubuntu and I compile with g++.
I'm pretty sure a library like that exists because it's too useful, I researched it but couldn't find anything.
I was trying 2 write a program that would calculate the sum notation of 1/(i^2) with the starting number to be 1 and goes up to the nth term. For instance if the user inputed 3 then the sum would look like 1+1/4+1/9. I somehow made a code but it gets weird numbers which some include negative numbers... when I input a number that is above 5.
#include <stdio.h> #include <math.h> int main(int argc, const char * argv[]) { int n; register int i=1; float b;//For part 1
[Code] ....
For some reason I can't edit printf("%f",/>/>; when I post it as the topic so ignore that part cuz Ik its supposed to be written as printf("%f",/>;
I'm working on a Fraction Class assignment where I'm supposed to provide conversion operators to allow Fraction objects to be used in expressions containing float data values. The way I have it now, I'm getting an error that says operator float(const Fraction &) must contain 'void', which makes no sense to me. Lines 155 - 173.
// Fractions CLASS // Source: Fraction2012.cpp // Author: Warren H. Knox, Jr. // Date: 11/8/2012 #include <iostream> #include <cstdlib> using namespace std; class Fraction {
I'm trying to compare 2 fractions using operator overloading. The program crashes when this is called;
this is definition: bool operator == (const Fraction& f1, Fraction& f2) { if (f1==f2)return true; else return false; }
this is my calling in the main: Fraction f1, f2; cout<<"Enter in the format: 2/4 or 4/9 "; cout << "enter first fraction: "; f1.Input(); cout << "enter second fraction: "; f2.Input();
Fraction result: result = (f1 == f2);//i think problem lies here. result.Show();
and this is the prototype of the operator: friend bool operator == (const Fraction& f1, Fraction& f2);