C++ :: Type Promotion Rules For Fixed Point Class

Jun 19, 2014

I've come across an interesting question involving a fixed-point arithmetic class.

Suppose I've got a template that implements a fixed-point quantity. It has two characteristics: width and number of fraction bits:

Code:
template <typename RAWTYPE, unsigned int FRACBITS>
class Fixed
{
....
};

So if I want a 32-bit value with 12 bits of fraction I can declare:

Code:
Fixed<int32_t, 12> x;

Suppose I've implemented a freestanding operator+() for fixed values, and I want to be able to add different types together:

Code:
template <typename RAWTYPE1, unsigned int FRACBITS1, typename RAWTYPE2, unsigned int FRACBITS2>
???? operator+(Fixed<RAWTYPE1, FRACBITS1> lhs, Fixed<RAWTYPE2, FRACBITS2> rhs)
{
???
}

What is the result type? Obviously, it's up to me to decide this. As reference, consider the type promotion rules for native types:

Code:
short a;
int b;
int result = a + b; In this case, the short value is promoted to the int value, and the addition happens on int.

It would seem a similar rule (go to the wider type) would be appropriate for fixed point. But there is another dimension to the problem, which is the number of fraction bits. Should you go to the wider type? Or the most precise type? Should you endeavor to minimize the number of bits which are discarded? What's the most intuitive rule?

View 10 Replies


ADVERTISEMENT

C++ :: Fixed Point Int Min Value

May 14, 2013

I am reading about positive and negative infinity in c++ and i am trying to implement them in a fixed point math arthimethic implementation

I can see that max of a int will be equal to std::numeric_limits<int>::max();
and min value of the int will be equal to std::numeric_limits<int>::min();
in c++

Here as i am defining the int max and int min manually in my fixed point math implementation, my doubt is
int min = -int max; or int min = -int max -1; ?

View 1 Replies View Related

C :: Fixed Point From String

Oct 9, 2013

I have been writing a fixed point library the would handle fixed point numbers with an 8:24 whole/fraction ratio. This has been working quite well but since I have a 24 bit fractional part, it should be able to store 2^(-24).

Code:
long long fraction_part = 0;
long long divisor = 1;

while(*string) {
fraction_part *= 10;
fraction_part += *string - '0';
divisor *= 10;
string++;
}

fraction_part <<= 24;
fraction_part /= divisor;

The issue here is that since the smallest possible fraction is 2^(-24) the divisor could end up needing more than 64 bits and so won't work. I'm not quite sure how else I could do this.

View 7 Replies View Related

C++ :: Fixed Point Implementation - Calling A Static Member Function

Apr 25, 2013

I am doing fixed point implementation in c++ which is done by the following code

#ifndef __fixed_point_header_h__
#define __fixed_point_header_h__
#include <boost/assert.hpp>
#include <boost/static_assert.hpp>
#include <boost/operators.hpp>
#include <limits>

[Code] ....

I am getting the error ambiguous overload for âoperator<â in âbeta < ((-5.0e-1) * pi)â

I know the problem the static member function cannot access the members and objects of structs. am i right?

and how to solve this problem, do i need to implement the cossin_cordic function as a non member function.

View 14 Replies View Related

C++ :: Creating Array Of Pointers To Base Class To Point To Derived Class Objects Dynamically

Jan 16, 2013

Please consider the following code :

#include <iostream>
using namespace std;
class superclass;
class subclass1;
class subclass2;

[Code] ....

As you can see I want to create a dynamically allocated storage of references to a parent class each of which can then point to a child class, how ever I do not know how to extract the child class out again from that array so i may access its variable b.

View 2 Replies View Related

C/C++ :: Implementing Container Class That Holds Point Class

Nov 28, 2014

I have a Point class that's already implemented. My goal is to implement a container class called Line that holds the Point class. I'm not allowed to use any existing container classes for this. Here's what I'm working with:

//File: Point.h
#ifndef POINT_H_
#define POINT_H_
class Point {
public:
Point(int x = 0, int y = 0);
Point(const Point & t);
virtual ~Point() {};

[Code] ....

How I'm supposed to write Line.cpp...how do I access/add Points to Line without using something like a vector? I probably should've included what I've written so far.

#include "Line.h"
/**
* Default Line constructor
*/
Line::Line() {

[Code] ....

View 6 Replies View Related

C/C++ :: Programming Variable Name Rules?

Nov 13, 2014

Where are the c programming variable name rules defined? I usually use these two websites for figuring out these kind of things but I don't see it anywhere.

[URL]

View 4 Replies View Related

C++ :: How To Initialize Static Member Of Class With Template And Type Of Nested Class

Oct 7, 2014

How to initialize a static member of a class with template, which type is related to a nested class?

This code works (without nested class):

#include<iostream>
using namespace std;
struct B{
B(){cout<<"here"<<endl;}
};
template<typename Z>

[Code] ,....

View 1 Replies View Related

C++ :: Rules To Compare Two Strings Object

Sep 1, 2014

In my book I have the following example : Code:

std::string str = "Hello";
std::string phrase = "Hello world";
std::string slang = "Hiya"; and i have these two rules to compare 2 strings object :

if two strings have different lenghts and if every character in the shorter string is equal to the corresponding character of the longer string, than the shorter string is less than the longer string.

if any characters at corresponding positions of two strings differ, then the result of the string comparison is the result of comparing the first character at wich the strings differ then my book says : if we apply the rules of the comparison we know that phrase is greater than str( ok i've understood this ) and that slang is greater than both slang and phrase ( why ?)

explain me rule number two ? in phrase and slang the characters differ and the first character that differ is not H so why my book says slang is bigger than phrase ?

View 3 Replies View Related

C++ :: Declare Template Type Object Inside Template Type Class Definition

Oct 12, 2013

Let me put it into the code snippet:

/**
This class build the singleton design pattern.
Here you have full control over construction and deconstruction of the object.
*/
template<class T>
class Singleton

[Code]....

I am getting error at the assertion points when i call to the class as follows:

osgOpenCL::Context *cxt = osgOpenCL::Singleton<osgOpenCL::Context>::getPtr();

I tried commenting assertion statements and then the debugger just exits at the point where getPtr() is called.

View 7 Replies View Related

C :: Read A Text File That Contains Permutation Rules

Mar 27, 2014

I have been given an assignment that has to do with permutations. I am suppose to read a text file that contains the permutation rules and the text to be "permutated", and then output the rules and the new text into an output file.

So far, I've gotten this:

Code:
# include <stdio.h>
void printArray(FILE* file, char* array, int maxSize)
{
int i;
for (i = 0; i<maxSize; i++)
fprintf(file, "%c", *(array + i));

[Code] ....

Here is what the input file looks like:

Code:
0 1 2 3 4 5 6 7 8 9
4 5 6 0 9 7 8 1 2 3

Moderation in temper, is always a virtue; but moderation in principle, is a species of vice.

Here is what the output file is suppose to look like:

Code:
0 1 2 3 4 5 6 7 8 9
4 5 6 0 9 7 8 1 2 3

ratMnioodetem rpein al,ywa isvirsetu a t m;eod
buon r inaticipp,lerina s cpeis of icvies - Temho.
-ainaPes

The first two lines are the permutation rules. Currently I have figured out how to read the file into an array and then print it back out into a text.

What I want to do is figure out how to read only the first two line of the input file and store that as a permutation rules, and then continue reading the rest of the input file and store that separately as the text to be "permutated". And then eventually figure out how to apply the permutation to the text.

View 3 Replies View Related

C++ :: Create Password Check Program That Makes Sure That Rules Are Followed

Feb 27, 2014

I am new to C++ and am stuck on a program. I've got to create a password check program that makes sure the password rules are followed. Below is the code and the rules i have typed in the comments at the beginning of the program. I have to use loops and cant use arrays for this.

#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main() {
//Prompt user to entered a password to be tested
cout << "Password must be at least 8 characters long." << endl;

[Code] .....

View 2 Replies View Related

C++ :: Templated Class Function Does Not Have Class Type

Feb 3, 2013

I'm trying to template the return type for this function (component), I've looked around for example code but there doesn't seem to be any exactly like what I want.

Entity.hpp
class Entity {
public:
Entity();
unsigned int id = 0;
Component& addComponent(std::string);

[Code] ....

Error : 'ent1.component<HealthComponent>' does not have class type

View 2 Replies View Related

C++ :: Passing Class As Type In Template Class

Nov 30, 2013

I am trying to pass a class as a type to a template class. This class's constructor needs an argument but I cannot find the correct syntax. Is it possible?

Here is an example of what I described above. I did not compiled it, it is for illustrative purpose only. And of course argument val of the myData constructor would be doing something more useful than simply initializing an int....

template <class T>
class templateClass {
templateClass() {};

[Code]....

My real code would only compile is I add the myData constructor:

myData () {};

and gdb confirmed that it is this constructor that get called, even with dummy(4).

View 4 Replies View Related

C :: Program That Queries User For A Noun And Forms Its Plural On The Basis Of Rules

Nov 13, 2014

I am having trouble with a program that queries the user for a noun and forms its plural on the basis of these rules:

a. If the noun ends in "y" remove the "y" and add "ies".
b. If the noun ends in "s", "ch", or "sh" add "es".
c. In all other cases, add "s".

I am having trouble getting started.

View 4 Replies View Related

C :: How To Change MPI Broadcast Into Asynchronous Point To Point Communication

Jun 26, 2013

I have one code that use MPI broadcast and I want to change it into Asynchronous Point to Point communication. I am newbie in Parallel programming. Looking for implementation of one simple same program in broadcast and P2P ?

View 6 Replies View Related

C++ :: How To Set Fixed Length For String

May 28, 2013

The input consists of one or more packets followed by a line containing only # that signals the end of the input. Each packet is on a line by itself, does not begin or end with a space, and contains from 1 to 255 characters.

it said 1 to 255 characters

i have to use getline(cin,str);

i tried str[255] but some error happen

View 2 Replies View Related

C++ :: How To Get Keypress Within Fixed Amount Of Time

Aug 22, 2013

the program has to accept a keypress. It should wait for some fixed amount of time. If a key is pressed within this time, the program should call a function. If a key is not pressed in this time limit the program should continue its normal execution. The problem with getch() is that it essentially requires you to press a key and it does not allow other instructions to execute until the key is pressed.

View 3 Replies View Related

C++ :: How To Create Unordered Map Of Fixed Size Vectors

Sep 19, 2013

how to create an unordered map of fixed size vectors?

Code:

unordered_map<string, vector<int>> x;

x["mumb 5"][7] = 65; // this does not work since my vector size is not set.

View 7 Replies View Related

C++ :: Shaders Running Slower Than Fixed Pipeline

Feb 3, 2014

While testing some simple examples with glDrawArrays I noticed switching it to shaders can cut my frame rate by over half (from 600- 300). I know I am using a bunch of deprecated code in GLSL right now but I don't expect it to cause that much of an fps drop. I would say it was just my intel graphics chip but if the FFP can handle it I see no reason shaders can't.

--Windows 7 Premum 64-bit
--Intel Core i3 540
--Intel HD Graphics

//Sending 10,000 quads with GLfloats (So Vertices vector has 80,000 elements, currently no indexing).
//Vertices allocated earlier in code, before main game loop

glBindBuffer(GL_ARRAY_BUFFER, vbo);
glEnableClientState(GL_VERTEX_ARRAY );

glVertexPointer(2, GL_FLOAT, 0, 0);
glDrawArrays(GL_QUADS, 0, Vertices.size() / 2);

[Code] .....

The highest Opengl version I can run is 2.1 so 120 in shaders. I know this is a fairly pointless test right now but it is still surprising to see, anything obvious I am missing?

View 19 Replies View Related

C/C++ :: Plotting Points On A Circle With One Fixed Axis

Oct 31, 2014

I am working on a computer program where I need to generate points on a circle. I am familiar with this kind of algorithm:

for(d=0; d<=2*pi; d+=0.01)
{
x = cos(d)*radius;
y = sin(d)*radius;
}

However, due to the specifics of the program I am writing, I need to iterate through a fixed number of points one at a time, like so:

for ( int x = 0; x < blockSize; x++ )
{
y = ???
}

This essentially "fixes" one axis of the circle, since I can't do: x=rx+sin(d)*r.

I have tried simply: "y = sin(d)*radius;" and I get a curved shape, but it's not a circle.

My question then is, how do I get the value of y in this situation, where the x axis is incrementing by 1 through a range of values? Is it mathematically possible?

View 6 Replies View Related

C# :: How To Assign Distance Values To Fixed Graph

Apr 28, 2014

I'm working on a Dijkstra's algorithm with a fixed graph. My question is, how should I go about assign distance values to the graph. The user can start from any node.

View 4 Replies View Related

C++ :: Passing Fixed Character Array Into Binary File?

Apr 19, 2014

all i want to do is to read a fixed char array sized 4 from user and pass it to Binary File then Print Encrypted content from the the File to the console screen .. but it seems it prints the same input every time .. and i tried everything .. it works fine with integers and strings .. but when it come to char array nothing ..

#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;

[Code].....

View 6 Replies View Related

C++ :: Char Vector - Fixed Size Two Dimension Array

Jun 9, 2013

I want to save the char[8][8] // fixed size 2 dimension array

to a vector

such as

vector<?????> temp;

is there anyway to approach this?

View 4 Replies View Related

C++ :: Expression Must Have A Class Type?

Apr 3, 2014

I am making a program that allows you to add two big numbers that are larger then what int can handle together. I think I have done everything to accomplish this but when I try to instantiate the program I get a error Expression must have a class type.

Here is my main file that is supposed to instantiate the program.

Code: #include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <conio.h>
#include "LargeIntegers.h"
using namespace std;

[code]....

View 10 Replies View Related

C++ :: Return Value For A Type Class?

Apr 25, 2014

Code:
returnType operatorOperatorSymbol (parameterList); // syntax for operator overloading
Class Fraction

public:
Fraction operator-(const Fraction& f1){
Fraction r;
return r;
}

Is this even syntactically correct? It gives me errors. Im just trying to compile it without errors. I think the function makes sense since its returning a type Class

View 2 Replies View Related







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