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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:11:28+00:00 2026-06-01T05:11:28+00:00

I have a 3-vector, let’s say v = vec3(-4, 2, 3) I would like

  • 0

I have a 3-vector, let’s say

v = vec3(-4, 2, 3)

I would like to do a max on the absolute values of the components, so the equivalent of:

max(abs(v[0]), max(abs(v[1]), abs(v[2]))) == 4

However, I have a requirement that I need to preserve the sign. So for example:

magic_max(v[0], magic_max(v[1], v[2])) == -4.

It’s a trivial problem if I use conditional branching, but I’m trying to do this in as few operations as possible, and avoid branching. Any ideas on where to look? Maybe there’s some bit-shifting magic that can be done?

  • 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-06-01T05:11:29+00:00Added an answer on June 1, 2026 at 5:11 am

    I would determ the max AND the min of all values, and then decide what is abs larger

    ma = max(v[0], max(v[1], v[2]));
    mi = min(v[0], min(v[1], v[2]));
    
    res = abs(mi) > ma ? mi : ma;
    

    If you want to get the sign, replace the last line with an if

    if (abs(mi) > ma) {
      sign = -1;
      res = mi;
    } else {
      sign = +1;
      res = ma;
    }
    

    However, what should happen on (0, 0, 0)? no sign?

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

Sidebar

Related Questions

Let's say I have an array of lots of values (C++ syntax, sorry): vector<double>
Let's say I have a upright capsule shape (swept sphere) that I would like
I have a numeric vector, let's say something like: x <- rep(1:6, 300) What
Let say I have a basic 2D vector class something like class vector2 {
I have a large nested vector that look like this: import Data.Vector let x
Let's assume that we have a vector like x = -1:0.05:1; ids = randperm(length(x));
Let's say I have a container (std::vector) of pointers used by a multi-threaded application.
Let's say i have 2 classes: class Class1 { public: std::vector<CustomClass3*> mVec; public: Class1();
Let's say I have: vector<T *> vect; I know it's not the obvious way
It may be a bit confusing, but... Let's say I have a vector type

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.