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.

View 5 Replies


ADVERTISEMENT

C++ :: Most Efficient Way To Read A Bit From A Byte?

Mar 30, 2013

What is the most efficient way to read a bit at a particular position in a byte?

View 3 Replies View Related

C++ :: Efficient Algorithm On List Iterator

Mar 6, 2013

I have a list and a vector, for example like

list<int> mylist = {1, 1, 1, 0, 0, 1, 0, 0}
vector<int> myvec;

I want to create an efficient algorithm to erase all element of value "1", and give these elements to a vector. My code (not efficient) is as follows

for(auto it = mylist.begin(); it != mylist.end(); it++) {
if(*it == 1) {
myvec.push_back(*it);
it = mylist.erase(it);
if(it != mylist.begin())
it--;
}
}

View 8 Replies View Related

C++ :: Fastest Way To Calculate Fibonacci

Apr 1, 2013

What is the most efficient way to calculate Fibonacci. Is it recursion with memorization, is it iteration or is there another approach?

View 1 Replies View Related

C++ :: Fastest Way Of Passing Data To A Class

Apr 25, 2013

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.

View 14 Replies View Related

C :: Fastest Way To Check If Bytes Contained In Array Are All X00

Nov 30, 2014

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?

View 1 Replies View Related

C++ :: Absolute Fastest Container To Lookup Names

Jan 9, 2013

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.

View 2 Replies View Related

C :: Fastest Way Of Changing Elements In Array Of Characters For Encode

Feb 15, 2014

I am wonder what is the fastest way of changing elements in array of characters for encode purpose, here is simple example presenting an idea:

Code:

char *a = "123456789":
char b[] = "ABCDEFGHI";
int 1;
for (i=0;i<strlen(a);i++) {

b[i] = a[i];
}

Is this approach better or worse than bit manipulation ?

View 4 Replies View Related

C++ :: Fastest Comparison Algorithm Based On A Predefined Container?

Sep 3, 2014

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?

Example:
master = {tom, sam, mary, sally, bert, frank}
subset = {bert, sam, mary, tom}
goal: change subset to {tom, sam, mary, bert}

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:

#include <iostream>
#include <iterator>
#include <list>
#include <vector>
#include <stdexcept>
#include <algorithm>

[code]....

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?

View 19 Replies View Related

C :: Code Not Accepting Fractions?

Dec 12, 2014

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 );
}

[code]....

View 7 Replies View Related

C++ :: Program To Calculate Fractions?

Oct 24, 2013

I need to write a program that can calculate fractions.

View 2 Replies View Related

C++ ::  Adding Fractions - How To Align Correctly

Mar 18, 2014

Ii need an output like this...

#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <ctype.h>
#include <locale>
using namespace std;

int EquationNum (int Num1, int Den1, int Num2, int Den2);

[Code] ....

View 9 Replies View Related

C++ :: Work With Fractions And Irrational Numbers

Feb 17, 2013

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.

View 4 Replies View Related

C/C++ :: Program For Adding Many Fractions With Sum Notation

Jul 16, 2014

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",/>;

View 6 Replies View Related

C++ :: Finding Sum Of Two Fractions - Return Array In Function?

Jul 30, 2013

I am making a function that finds the sum of two fractions(fractions are arrays of 2 elements). how would I return an array?

int addFrac(int f1[2], int f2[2]){
int num1 = f1[0]; // numerator of fraction 1
int den1 = f1[1]; // denominator of fractions 1

[Code] ......

View 2 Replies View Related

C/C++ :: Converting Fractions To Decimals And Vice Versa

Aug 1, 2014

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 {

[Code] ....

View 9 Replies View Related

C++ :: Operator Overloading - Comparing Objects (Fractions)

Jul 25, 2013

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);

View 7 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved