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 548943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:07:59+00:00 2026-05-13T11:07:59+00:00

I’m trying to achieve something like the following in C++: class MyVector; // 3

  • 0

I’m trying to achieve something like the following in C++:

class MyVector; // 3 component vector  class

MyVector const kA = /* ... */;
MyVector const kB = /* ... */;

MyVector const kC = /* ... */;
MyVector const kD = /* ... */;


// I'd like to shorten the remaining lines, ideally making it readable but less code/operations.
MyVector result = kA;

MyVector const kCMinusD = kC - kD;

if(kCMinusD.X <= 0)
{
    result.X = kB.X;
}

if(kCMinusD.Y <= 0)
{
    result.Y = kB.Y;
}

if(kCMinusD.Z <= 0)
{
    result.Z = kB.Z;
}

Paraphrasing the code into English, I have four ‘known’ vectors. Two of the vectors have values that I may or may not want in my result, and whether I want them or not is contingent on a branch based on the components of two other vectors.

I feel like I should be able to simplify this code with some matrix math and masking, but I can’t wrap my head around it.

For now I’m going with the branch, but I’m curious to know if there’s a better way that still would be understandable, and less code-verbose.

Edit:

In reference to Mark’s comment, I’ll explain what I’m trying to do here.

This code is an excerpt from some spring physics I’m working on. The components are as follows:

kC is the springs length currently, and kD is minimum spring length.

kA and kB are two sets of spring tensions, each component of which may be unique per component (i.e., a different spring tension along the X, Y, or Z). kA is the springs tension if it’s not fully compressed, and kB is the springs tension if it IS fully compressed.

I’d like to build up a resultant ‘vector’ that simply is the amalgamation of kC and kD, dependant on whether the spring is compressed or not.

  • 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-13T11:07:59+00:00Added an answer on May 13, 2026 at 11:07 am

    Depending on the platform you’re on, the compiler might be able to optimize statements like

    result.x = (kC.x > kD.x) ? kA.x : kB.x;
    result.y = (kC.y > kD.y) ? kA.y : kB.y;
    result.z = (kC.z > kD.z) ? kA.z : kB.z;
    

    using fsel (floating point select) instructions or conditional moves. Personally, I think the code looks nicer and more concise this way too, but that’s subjective.

    If the code is really performance critical, and you don’t mind changing your vector class to be 4 floats instead of 3, you could use SIMD (e.g SSE on Intel platforms, VMX on PowerPC) to do the comparison and select the answers. If you went ahead with this, it would like this: (in pseudo code)

    // Set each component of mask to be either 0x0 or 0xFFFFFFFF depending on the comparison
    MyVector4 mask = vec_compareLessThan(kC, kD);
    
    // Sets each component of result to either kA or kB's component, depending on whether the bits are set in mask
    result = vec_select(kA, kb, mask);
    

    This takes a while getting used to, and it might be less readable initially, but you eventually get used to thinking in SIMD mode.

    The usual caveats apply, of course – don’t optimize before you profile, etc.

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

Sidebar

Related Questions

No related questions found

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.