Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4084908
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:29:59+00:00 2026-05-20T18:29:59+00:00

I have a vector class that has it’s Equals(object obj) method overridden so that

  • 0

I have a vector class that has it’s Equals(object obj) method overridden so that I can compare them.

public class Vector3f
{
    public float x,y,z;
    public Vector3f(float x, float y, float z)
    {
       this.x = x;
       this.y = y;
       this.z = z;
    }

    public static Vector3f operator +(Vector3f a, Vector3f b) {
        return new Vector3f(a.x + b.x, a.y + b.y, a.z + b.z);
    }

    public static Vector3f operator -(Vector3f a, Vector3f b) {
        return new Vector3f(a.x - b.x, a.y - b.y, a.z - b.z);
    }
    public override bool Equals(object obj)
    {
        Vector3f other = (Vector3f)obj;
        return x == other.x && y == other.y && z == other.z;
    }

    public override string ToString()
    {
        return String.Format("<{0},{1},{2}>",x,y,z);
    }
}

The plus operator works as expected in my unit tests. However, when I subtract two vectors it says they are not equal

Test 'RTTests.Testies.vector_subtraction_works' failed: 
Expected: <<1.1,0.1,0.1>>
But was:  <<1.1,0.1,0.1>>
Testies.cs(60,0): at RTTests.Testies.vector_sub_works()

I’m not sure why the comparison is working for addition and not subtraction especially since the output values are identical in both cases?

EDIT: My tests for this

    [Test]
    public void vector_addition_works()
    {
        Vector3f v1 = new Vector3f(1.0f, 1.0f, 1.0f);
        Vector3f v2 = new Vector3f(1.6f, 3.2f, 4.7f);

        Vector3f expected = new Vector3f(2.6f, 4.2f, 5.7f);
        Vector3f actual = v1 + v2;

        Assert.AreEqual(actual, expected);
    }

    [Test]
    public void vector_sub_works()
    {
        Vector3f v1 = new Vector3f(1.1f, 1.1f, 1.1f);
        Vector3f v2 = new Vector3f(0.0f, 1.0f, 1.0f);

        Vector3f expected = new Vector3f(1.1f, 0.1f, 0.1f);
        Vector3f actual = v1 - v2;

        Assert.AreEqual(actual, expected);
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T18:30:00+00:00Added an answer on May 20, 2026 at 6:30 pm

    Your problem must be a rounding/truncation error. It happens all the time with floating point operations, specially subtraction. When you test for equality, instead of a==b, use a-b < SmallConstant. You could also try using double precision or Decimal, although truncation errors will eventually return, but you might make them less common.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that has a vector of another class objects as a
I have a vector class that has addition, multiplication, subtraction, division, etc operators. I'm
i'm lost in this , i have a class that has three vector objects
I have a class that has a vector of objects. What do I need
Say you have a vector class that has a template length and type -
I have a class that contains a vector of object pointers. I have a
I have a vector-like class that contains an array of objects of type T
I have a vector class in C# (a fragment below). My issue is that
I have a class Message that has a std::string as a data member, defined
I have a tree of objects, where each object has a std::vector of pointers

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.