Search Results

Search found 2644 results on 106 pages for 'diamond operator'.

Page 15/106 | < Previous Page | 11 12 13 14 15 16 17 18 19 20 21 22  | Next Page >

  • why there is no power operator in java / c ++?

    - by RanZilber
    While there is such operator - ** in Python , i was wondering why java and c++ havent got one too. It is easy to make one for classes you define in C++ with operator overloading ( and i believe such thing is possible also in java) , but when talking about primitive types such as int, double and so on , you'll have to use library function like Math.power (and usaully have to cast both to double). So - why not define such operator for primitive types ?

    Read the article

  • Trying to reduce the speed overhead of an almost-but-not-quite-int number class

    - by Fumiyo Eda
    I have implemented a C++ class which behaves very similarly to the standard int type. The difference is that it has an additional concept of "epsilon" which represents some tiny value that is much less than 1, but greater than 0. One way to think of it is as a very wide fixed point number with 32 MSBs (the integer parts), 32 LSBs (the epsilon parts) and a huge sea of zeros in between. The following class works, but introduces a ~2x speed penalty in the overall program. (The program includes code that has nothing to do with this class, so the actual speed penalty of this class is probably much greater than 2x.) I can't paste the code that is using this class, but I can say the following: +, -, +=, <, > and >= are the only heavily used operators. Use of setEpsilon() and getInt() is extremely rare. * is also rare, and does not even need to consider the epsilon values at all. Here is the class: #include <limits> struct int32Uepsilon { typedef int32Uepsilon Self; int32Uepsilon () { _value = 0; _eps = 0; } int32Uepsilon (const int &i) { _value = i; _eps = 0; } void setEpsilon() { _eps = 1; } Self operator+(const Self &rhs) const { Self result = *this; result._value += rhs._value; result._eps += rhs._eps; return result; } Self operator-(const Self &rhs) const { Self result = *this; result._value -= rhs._value; result._eps -= rhs._eps; return result; } Self operator-( ) const { Self result = *this; result._value = -result._value; result._eps = -result._eps; return result; } Self operator*(const Self &rhs) const { return this->getInt() * rhs.getInt(); } // XXX: discards epsilon bool operator<(const Self &rhs) const { return (_value < rhs._value) || (_value == rhs._value && _eps < rhs._eps); } bool operator>(const Self &rhs) const { return (_value > rhs._value) || (_value == rhs._value && _eps > rhs._eps); } bool operator>=(const Self &rhs) const { return (_value >= rhs._value) || (_value == rhs._value && _eps >= rhs._eps); } Self &operator+=(const Self &rhs) { this->_value += rhs._value; this->_eps += rhs._eps; return *this; } Self &operator-=(const Self &rhs) { this->_value -= rhs._value; this->_eps -= rhs._eps; return *this; } int getInt() const { return(_value); } private: int _value; int _eps; }; namespace std { template<> struct numeric_limits<int32Uepsilon> { static const bool is_signed = true; static int max() { return 2147483647; } } }; The code above works, but it is quite slow. Does anyone have any ideas on how to improve performance? There are a few hints/details I can give that might be helpful: 32 bits are definitely insufficient to hold both _value and _eps. In practice, up to 24 ~ 28 bits of _value are used and up to 20 bits of _eps are used. I could not measure a significant performance difference between using int32_t and int64_t, so memory overhead itself is probably not the problem here. Saturating addition/subtraction on _eps would be cool, but isn't really necessary. Note that the signs of _value and _eps are not necessarily the same! This broke my first attempt at speeding this class up. Inline assembly is no problem, so long as it works with GCC on a Core i7 system running Linux!

    Read the article

  • Perl ||= operator for PHP and Javascript

    - by zaf
    Just been re-introduced to the Perl '||=' operator from the classic Orcish Maneuver example: keys my %or_cache = @in; @out = sort { ($or_cache{$a} ||= KEY($a)) cmp ($or_cache{$b} ||= KEY($b)) } @in; Is this operator available in PHP and Javascript? And if not, do these two languages allow user defined operators?

    Read the article

  • undefined C/C++ symbol as operator

    - by uray
    I notice that the character/symbol '`' and '@' is not used as an operator in C/C++, does anyone know the reason or historically why its so? if its really not used, is it safe to define those symbols as another operator/statement using #define?

    Read the article

  • Javascript IN operator compatibility

    - by jAndy
    Hi Folks, Can someone tell me since which ECMA version the IN operator is available and which browsers (versions) support it ? Explanation: The IN-operator can be used like the following: var myObject = { Firstname: 'Foo', Lastname: 'Bar' }; if('Lastname' in myObject){ // Lastname is an attribute of myObject }

    Read the article

  • c++ global operator not playing well with template class

    - by John
    ok, i found some similar posts on stackoverflow, but I couldn't find any that pertained to my exact situation and I was confused with some of the answers given. Ok, so here is my problem: I have a template matrix class as follows: template <typename T, size_t ROWS, size_t COLS> class Matrix { public: template<typename, size_t, size_t> friend class Matrix; Matrix( T init = T() ) : _matrix(ROWS, vector<T>(COLS, init)) { /*for( int i = 0; i < ROWS; i++ ) { _matrix[i] = new vector<T>( COLS, init ); }*/ } Matrix<T, ROWS, COLS> & operator+=( const T & value ) { for( vector<T>::size_type i = 0; i < this->_matrix.size(); i++ ) { for( vector<T>::size_type j = 0; j < this->_matrix[i].size(); j++ ) { this->_matrix[i][j] += value; } } return *this; } private: vector< vector<T> > _matrix; }; and I have the following global function template: template<typename T, size_t ROWS, size_t COLS> Matrix<T, ROWS, COLS> operator+( const Matrix<T, ROWS, COLS> & lhs, const Matrix<T, ROWS, COLS> & rhs ) { Matrix<T, ROWS, COLS> returnValue = lhs; return returnValue += lhs; } To me, this seems to be right. However, when I try to compile the code, I get the following error (thrown from the operator+ function): binary '+=' : no operator found which takes a right-hand operand of type 'const matrix::Matrix<T,ROWS,COLS>' (or there is no acceptable conversion) I can't figure out what to make of this. Any help if greatly appreciated!

    Read the article

  • Why isn't the boost::shared_ptr -> operator inlined?

    - by Alan
    Since boost::shared_ptr could be called very frequently and simply returns a pointer, isn't the -> operator a good candidate for being inlined? T * operator-> () const // never throws { BOOST_ASSERT(px != 0); return px; } Would a good compiler automatically inline this anyway? Should I lose any sleep over this? :-)

    Read the article

  • What does the C# operator => mean?

    - by Mr. Mark
    Answers to a recent post (Any chances to imitate times() Ruby method in C#?) use the = operator in the usage examples. What does this operator do? I can't locate it in my C# book, and it is hard to search for symbols like this online. (I couldn't find it.)

    Read the article

  • Is return an operator or a function?

    - by eSKay
    This is too basic I think, but how do both of these work? return true; // 1 and return (true); // 2 Similar: sizeof, exit My guess: If return was a function, 1 would be erroneous. So, return should be a unary operator that can also take in brackets... pretty much like unary minus: -5 and -(5), both are okay. Is that what it is - a unary operator?

    Read the article

  • PHP: Object Oriented Programming -> Operator

    - by oman9589
    So I've been reading through the book PHP Solutions, Dynamic Web Design Made Easy by David Powers. I read through the short section on Object Oriented PHP, and I am having a hard time grasping the idea of the - operator. Can anyone try to give me a solid explanation on the - operator in OOP PHP? Example: $westcost = new DateTimeZone('America/Los_Angeles'); $now->setTimezone($westcoast); Also,a more general example: $someObject->propertyName Thanks

    Read the article

  • binary operator "<"

    - by md004
    Consider this expression as a "selection" control structure on integer "x": 0 < x < 10, with the intention that the structure returns TRUE if "x" is in the range 1..9. Explain why a compiler should not accept this expression. (In particular, what are the issues regarding the binary operator "<"? Explain how a prefix operator could be introduced so the expression can be successfully processed.

    Read the article

  • Polynomial operations using operator overloading

    - by Vlad
    I'm trying to use operator overloading to define the basic operations (+,-,*,/) for my polynomial class but when i run the program it crashes and my computer frozes. Update3 Ok i successfully done the first two operations(+,-). Now at multiplication, after multiplying each term of the first polynomial with each of the second i want to sort the poly list descending and then if there are more than one term with the same power to merge them in only one term, but for some reason it doesn't compile because of the sort function which doesn't work. Here's what I got: polinom operator*(const polinom& P) const { polinom Result; constIter i, j, lastItem = Result.poly.end(); Iter it1, it2; int nr_matches; for (i = poly.begin() ; i != poly.end(); i++) { for (j = P.poly.begin(); j != P.poly.end(); j++) Result.insert(i->coef * j->coef, i->pow + j->pow); } sort(Result.poly.begin(), Result.poly.end(), SortDescending()); lastItem--; while (true) { nr_matches = 0; for (it1 = Result.poly.begin(); it < lastItem; it1++) { for (it2 = it1 + 1;; it2 <= lastItem; it2++){ if (it2->pow == it1->pow) { it1->coef += it2->coef; nr_matches++; } } Result.poly.erase(it1 + 1, it1 + (nr_matches + 1)); } return Result; } Also here's SortDescending: struct SortDescending { bool operator()(const term& t1, const term& t2) { return t2.pow < t1.pow; } }; What did i do wrong? Thanks!

    Read the article

  • Template operator linker error

    - by Dani
    I have a linker error I've reduced to a simple example. The build output is: debug/main.o: In function main': C:\Users\Dani\Documents\Projects\Test1/main.cpp:5: undefined reference tolog& log::operator<< (char const (&) [6])' collect2: ld returned 1 exit status It looks like the linker ignores the definition in log.cpp. I also cant put the definition in log.h because I include the file alot of times and it complains about redefinitions. main.cpp: #include "log.h" int main() { log() << "hello"; return 0; } log.h: #ifndef LOG_H #define LOG_H class log { public: log(); template<typename T> log &operator <<(T &t); }; #endif // LOG_H log.cpp: #include "log.h" #include <iostream> log::log() { } template<typename T> log &log::operator <<(T &t) { std::cout << t << std::endl; return *this; }

    Read the article

  • How to make std::vector's operator[] compile doing bounds checking in DEBUG but not in RELEASE

    - by Edison Gustavo Muenz
    I'm using Visual Studio 2008. I'm aware that std::vector has bounds checking with the at() function and has undefined behaviour if you try to access something using the operator [] incorrectly (out of range). I'm curious if it's possible to compile my program with the bounds checking. This way the operator[] would use the at() function and throw a std::out_of_range whenever something is out of bounds. The release mode would be compiled without bounds checking for operator[], so the performance doesn't degrade. I came into thinking about this because I'm migrating an app that was written using Borland C++ to Visual Studio and in a small part of the code I have this (with i=0, j=1): v[i][j]; //v is a std::vector<std::vector<int> > The size of the vector 'v' is [0][1] (so element 0 of the vector has only one element). This is undefined behaviour, I know, but Borland is returning 0 here, VS is crashing. I like the crash better than returning 0, so if I can get more 'crashes' by the std::out_of_range exception being thrown, the migration would be completed faster (so it would expose more bugs that Borland was hiding).

    Read the article

  • c# "==" operator : compiler behaviour with different structs

    - by Moe Sisko
    Code to illustrate : public struct MyStruct { public int SomeNumber; } public string DoSomethingWithMyStruct(MyStruct s) { if (s == null) return "this can't happen"; else return "ok"; } private string DoSomethingWithDateTime(DateTime s) { if (s == null) return "this can't happen"; // XX else return "ok"; } Now, "DoSomethingWithStruct" fails to compile with : "Operator '==' cannot be applied to operands of type 'MyStruct' and '<null>'". This makes sense, since it doesn't make sense to try a reference comparison with a struct, which is a value type. OTOH, "DoSomethingWithDateTime" compiles, but with compiler warning : "Unreachable code detected" at line marked "XX". Now, I'm assuming that there is no compiler error here, because the DateTime struct overloads the "==" operator. But how does the compiler know that the code is unreachable ? e.g. Does it look inside the code which overloads the "==" operator ? (This is using Visual Studio 2005 in case that makes a difference). Note : I'm more curious than anything about the above. I don't usually try to use "==" on structs and nulls.

    Read the article

  • Java Newbie can't do simple Math, operator error

    - by elguapo-85
    Trying to do some really basic math here, but my lack of understanding of Java is causing some problems for me. double[][] handProbability = new double[][] {{0,0,0},{0,0,0},{0,0,0}}; double[] handProbabilityTotal = new double[] {0,0,0}; double positivePot = 0; double negativePot = 0; int localAhead = 0; int localTied = 1; int localBehind = 2; //do some stuff that adds values to handProbability and handProbabilityTotal positivePot = (handProbability[localBehind][localAhead] + (handProbability[localBehind][localTied] / 2.0) + (handProbability[localTied][localAhead] / 2.0) ) / (handProbabilityTotal[localBehind] + (handProbability[localTied] / 2.0)); negativePot = (handProbability[localAhead][localBehind] + (handProbability[localAhead][localTied] / 2.0) + (handProbability[localTied][localBehind] / 2.0) ) / (handProbabilityTotal[localAhead] + (handProbabilityTotal[localTied] / 2.0)); The last two lines are giving me problems (sorry for their lengthiness). Compiler Errors: src/MyPokerClient/MyPokerClient.java:180: operator / cannot be applied to double[],double positivePot = ( handProbability[localBehind][localAhead] + (handProbability[localBehind][localTied] / 2.0) + (handProbability[localTied][localAhead] / 2.0) ) / (handProbabilityTotal[localBehind] + (handProbability[localTied] / 2.0) ); ^ src/MyPokerClient/MyPokerClient.java:180: operator + cannot be applied to double, positivePot = ( handProbability[localBehind][localAhead] + (handProbability[localBehind][localTied] / 2.0) + (handProbability[localTied][localAhead] / 2.0) ) / (handProbabilityTotal[localBehind] + (handProbability[localTied] / 2.0) ); ^ src/MyPokerClient/MyPokerClient.java:180: operator / cannot be applied to double, positivePot = ( handProbability[localBehind][localAhead] + (handProbability[localBehind][localTied] / 2.0) + (handProbability[localTied][localAhead] / 2.0) ) / (handProbabilityTotal[localBehind] + (handProbability[localTied] / 2.0) ); Not really sure what the problem is. You shouldn't need anything special for basic math, right?

    Read the article

  • Using overloaded operator== in a generic function

    - by Dimitri C.
    Consider the following code: class CustomClass { public CustomClass(string value) { m_value = value; } public static bool operator==(CustomClass a, CustomClass b) { return a.m_value == b.m_value; } public static bool operator!=(CustomClass a, CustomClass b) { return a.m_value != b.m_value; } public override bool Equals(object o) { return m_value == (o as CustomClass).m_value; } public override int GetHashCode() { return 0; /* not needed */ } string m_value; } class G { public static bool enericFunction1<T>(T a1, T a2) where T : class { return a1.Equals(a2); } public static bool enericFunction2<T>(T a1, T a2) where T : class { return a1==a2; } } Now when I call both generic functions, one succeeds and one fails: var a = new CustomClass("same value"); var b = new CustomClass("same value"); Debug.Assert(G.enericFunction1(a, b)); // Succeeds Debug.Assert(G.enericFunction2(a, b)); // Fails Apparently, G.enericFunction2 executes the default operator== implementation instead of my override. Can anybody explain why this happens?

    Read the article

  • Trying to overload + operator

    - by FrostyStraw
    I cannot for the life of me understand why this is not working. I am so confused. I have a class Person which has a data member age, and I just want to add two people so that it adds the ages. I don't know why this is so hard, but I'm looking for examples and I feel like everyone does something different, and for some reason NONE of them work. Sometimes the examples I see have two parameters, sometimes they only have one, sometimes the parameters are references to the object, sometimes they're not, sometimes they return an int, sometimes they return a Person object. Like..what is the most normal way to do it? class Person { public: int age; //std::string haircolor = "brown"; //std::string ID = "23432598"; Person(): age(19) {} Person operator+(Person&) { } }; Person operator+(Person &obj1, Person &obj2){ Person sum = obj1; sum += obj2; return sum; } I really feel like overloading a + operator should seriously be the easiest thing in the world except I DON'T KNOW WHAT I AM DOING. I don't know if I'm supposed to create the overload function inside the class, outside, if it makes a difference, why if I do it inside it only allows one parameter, I just honestly don't get it.

    Read the article

  • How can I direct edge to get out of the diamond on the right?

    - by Didier Trosset
    I have a simple dot diagram to show how to perform tests. PerformTests; PerformTests<---+ PerformTests -> TestsPassed; | | TestsPassed [shape="diamond"]; v | TestsPassed -> Release [label="Yes"]; TestsPassed | TestsPassed -> FixErrors [label="No"]; Y| N\ | FixErrors -> PerformTests; v FixErrors Release The diagram shows square boxes for all nodes, except TestPassed that has a diamond shape. My issue is here. I'd like the edge that goes outside of the diamond for No to be getting out of the diamond at the right (east) instead of oblique down-right (south-east). What I have What I want ^ ^ / \ / \ < > < >---> \ /\ \ / v \ v I've seen such compass_pt in the dot grammar, but cannot figure out how to use it. I what I want possible, and how to do it?

    Read the article

  • Visitor and templated virtual methods

    - by Thomas Matthews
    In a typical implementation of the Visitor pattern, the class must account for all variations (descendants) of the base class. There are many instances where the same method content in the visitor is applied to the different methods. A templated virtual method would be ideal in this case, but for now, this is not allowed. So, can templated methods be used to resolve virtual methods of the parent class? Given (the foundation): struct Visitor_Base; // Forward declaration. struct Base { virtual accept_visitor(Visitor_Base& visitor) = 0; }; // More forward declarations struct Base_Int; struct Base_Long; struct Base_Short; struct Base_UInt; struct Base_ULong; struct Base_UShort; struct Visitor_Base { virtual void operator()(Base_Int& b) = 0; virtual void operator()(Base_Long& b) = 0; virtual void operator()(Base_Short& b) = 0; virtual void operator()(Base_UInt& b) = 0; virtual void operator()(Base_ULong& b) = 0; virtual void operator()(Base_UShort& b) = 0; }; struct Base_Int : public Base { void accept_visitor(Visitor_Base& visitor) { visitor(*this); } }; struct Base_Long : public Base { void accept_visitor(Visitor_Base& visitor) { visitor(*this); } }; struct Base_Short : public Base { void accept_visitor(Visitor_Base& visitor) { visitor(*this); } }; struct Base_UInt : public Base { void accept_visitor(Visitor_Base& visitor) { visitor(*this); } }; struct Base_ULong : public Base { void accept_visitor(Visitor_Base& visitor) { visitor(*this); } }; struct Base_UShort : public Base { void accept_visitor(Visitor_Base& visitor) { visitor(*this); } }; Now that the foundation is laid, here is where the kicker comes in (templated methods): struct Visitor_Cout : public Visitor { template <class Receiver> void operator() (Receiver& r) { std::cout << "Visitor_Cout method not implemented.\n"; } }; Intentionally, Visitor_Cout does not contain the keyword virtual in the method declaration. All the other attributes of the method signatures match the parent declaration (or perhaps specification). In the big picture, this design allows developers to implement common visitation functionality that differs only by the type of the target object (the object receiving the visit). The implementation above is my suggestion for alerts when the derived visitor implementation hasn't implement an optional method. Is this legal by the C++ specification? (I don't trust when some says that it works with compiler XXX. This is a question against the general language.)

    Read the article

< Previous Page | 11 12 13 14 15 16 17 18 19 20 21 22  | Next Page >