473,378 Members | 1,607 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Problem with C++ operator for Vector

Hello,
I've got a problem with the redefinition of operator in C++. My intent
is to create a TVector that internally work with __int64, with an
external interface of double in order to optimize the operations. The
double value put in input should be muliplied with factor 1e10, in
order to limit
the loss of precision and divided with the same quantity in output.
To make this, I've supposed to can modify the operator() for the access
at the singular element of a Vector.
My test Vector class is like this

class Vector
{
private:
__int64 *Array;
public:
inline __int64& TVector::operator()(byte Index) {if(Index>FDim) throw
Exception("Out of bound Exception");return
(__int64)Array[Index]*1e10;};
};

In the program:
double Value = 3.43;
Vector V(3);
V(1) = Value:// Internally V.Array[1] =3.43e10;
V(1)=V(1)+1;
Value = V(1); // Value=4.43;
Obviously the adopted solution does not work as I hope. Please, could
someone help me with any kind of advise? I hope to have explain in good
way the problem.
Thanks in advance.

May 12 '06 #1
5 2252
Toto wrote:
I've got a problem with the redefinition of operator in C++. My intent
is to create a TVector that internally work with __int64, with an
external interface of double in order to optimize the operations. The
double value put in input should be muliplied with factor 1e10, in
order to limit
the loss of precision and divided with the same quantity in output.
To make this, I've supposed to can modify the operator() for the
access at the singular element of a Vector.
My test Vector class is like this

class Vector
{
private:
__int64 *Array;
public:
inline __int64& TVector::operator()(byte Index) {if(Index>FDim) throw
Exception("Out of bound Exception");return
(__int64)Array[Index]*1e10;};
You can't expect it to work on the left side of an assignment operator.
You need something else there. Since you cannot expose the contents of
your 'Array' to the user (as you tried), you need to write proxy classes,
which will have assignment from a double expression and the conversion
from 'double':

struct proxy {
__int64& lvalue;
proxy(__int64& lv) : lvalue(lv) {}
void operator = (double d) {
lvalue = d * 1e10;
}
operator double () {
return lvalue / 1e10;
}
};

struct const_proxy {
__int64 lvalue;
const_proxy(__int64 lv) : lvalue(lv) {}
operator double () {
return lvalue / 1e10;
}
};

and make your Vector return an instance of 'proxy':

const_proxy operator()(byte Index) const {
if (Index >= FDim) throw Exception("...");
return const_proxy(Array[Index]);
}

proxy operator()(byte Index) {
if (Index >= FDim) throw Exception("...");
return proxy(Array[Index]);
}
};

In the program:
double Value = 3.43;
Vector V(3);
V(1) = Value:// Internally V.Array[1] =3.43e10;
V(1)=V(1)+1;
Value = V(1); // Value=4.43;
Obviously the adopted solution does not work as I hope. Please, could
someone help me with any kind of advise? I hope to have explain in
good way the problem.


You have. Now I hope I have explained the solution well.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 12 '06 #2
Hi,

Toto schrieb:
I've got a problem with the redefinition of operator in C++. My intent
is to create a TVector that internally work with __int64, with an
external interface of double in order to optimize the operations. The
double value put in input should be muliplied with factor 1e10, in
order to limit
the loss of precision and divided with the same quantity in output.


1) you'll still lose a lot of precision, depending on the range of
the numbers you want to store. Especially if they differ by more
than a factor of 1e10
2) Do you really think that an __int64-wrapper class will perform
better than native CPU's doubles?

Markus, not much help with your prblem, I know ;-)
May 13 '06 #3
Thank you very much for your help. Now I'm trying your solution and
after I check whether it works.

May 15 '06 #4
Dear Victor,
I have tried your solution. It works very well!! It's wonderful!!!

However I have encountered a little problem. Please read the example:

void Function(void)
{
double Value = 0;
Vector V(3);
Vector V2(3);

V(3) = 3; //OK
Value = V(3); //OK
V(2) = V(3); //OK
V2(1) = V(3); //Error Message: Compiler could not generate operator=
for class proxy
}

Please, could you help me to understand the Error of the last case? In
my point of view the lasts two code rows are similar, because in both
cases there are 2 assignment from element of Vector. Thanks in advance.

Other question: I would like to implement this solution in order to
speed up my mathematical libraries. Could anyone suggest me a website
(or similar thinks) which contains
sourcecode (for example written in assembler) that implements
operations with Matrix
and Vector? Thanks a lot.

May 15 '06 #5
Toto wrote:
Dear Victor,
I have tried your solution. It works very well!! It's wonderful!!!

However I have encountered a little problem. Please read the example:

void Function(void)
{
double Value = 0;
Vector V(3);
Vector V2(3);

V(3) = 3; //OK
Value = V(3); //OK
V(2) = V(3); //OK
V2(1) = V(3); //Error Message: Compiler could not generate operator=
for class proxy
}

Please, could you help me to understand the Error of the last case? In
my point of view the lasts two code rows are similar, because in both
cases there are 2 assignment from element of Vector. Thanks in
advance.
The problem is that the proxy class contains a reference. The compiler
cannot guess whether you mean to re-seat a reference or assign the value
when performing operator==, and that's a guess at this point. Implement
your own operator== in the proxy class and do what's right.
Other question: I would like to implement this solution in order to
speed up my mathematical libraries. Could anyone suggest me a website
(or similar thinks) which contains
sourcecode (for example written in assembler) that implements
operations with Matrix
and Vector? Thanks a lot.


I don't know of any website with those things, but I strongly recommend
putting aside performance for now. Only optimize what you know is not
optimal. And you can only know by measuring. There are whole books
written on optimization and performance, perhaps you need to check them
out... "Efficient C++" is one of them. D. Knuth's "The Art of Computer
Programming" is a classic, of course.

V
--
Please remove capital As from my address when replying by mail
May 15 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: James Aguilar | last post by:
Hello all, I am trying to use an istringstream to do some input off of cin by lines. The following snippet does not work: char buf; cin.getline(buf, 90); istringstream line1(string(buf));
2
by: hvaisane | last post by:
Valgrind says ==11604== Invalid read of size 4 ==11604== at 0x8048ABB: main (foo.cc:36) ==11604== Address 0x1B92415C is 4 bytes inside a block of size 8 free'd ==11604== at 0x1B90514F:...
3
by: rahul8143 | last post by:
hello, I write a following program and have problem in understanding constructors and destructors. #include <iostream.h> class vector { public: double x; double y;
1
by: Joannes Vermorel | last post by:
I am currently trying to port a small open source scientfic library written in C++ to .Net. The code (including the VS solution) could be found at http://www.vermorel.com/opensource/selfscaling.zip...
10
by: toton | last post by:
Hi I have a class called Session, which stores a vector of Page, like vector<PageAlso each Page need's to know the Session to which it is attached. Thus I have, (the classes has many other...
13
by: Jeroen | last post by:
Hi all, I'm trying to implement a certain class but I have problems regarding the copy ctor. I'll try to explain this as good as possible and show what I tried thusfar. Because it's not about a...
15
by: vivekian | last post by:
Hi, I have this following class class nodeInfo ; class childInfo { public: int relativeMeshId ;
4
by: mkborregaard | last post by:
Hi, I have the weirdest problem, and I can not see what is going wrong. I have made a 2d container class, and am implementing an iterator for that class. However, the ++ operator is behaving very...
2
by: bingo | last post by:
Hi, All: I was really new to C++, so please forgive me if I asked stupid questions. I was trying to use this following Vector class(not written by me) to do some calculations : ...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.