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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:12:53+00:00 2026-06-03T00:12:53+00:00

I have a function like this: float_as_thousands_str_with_precision(value, precision) If I use it like this:

  • 0

I have a function like this:

float_as_thousands_str_with_precision(value, precision)

If I use it like this:

float_as_thousands_str_with_precision(volts, 1)
float_as_thousands_str_with_precision(amps, 2)
float_as_thousands_str_with_precision(watts, 2)

Are those 1/2s magic numbers?

  • 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-03T00:12:54+00:00Added an answer on June 3, 2026 at 12:12 am

    Yes, they are magic numbers. It’s obvious that the numbers 1 and 2 specify precision in the code sample but not why. Why do you need amps and watts to be more precise than volts at that point?

    Also, avoiding magic numbers allows you to centralize code changes rather than having to scour the code when for the literal number 2 when your precision needs to change.

    I would propose something like:

    HIGH_PRECISION = 3;
    MED_PRECISION = 2;
    LOW_PRECISION = 1;
    

    And your client code would look like:

    float_as_thousands_str_with_precision(volts, LOW_PRECISION )
    float_as_thousands_str_with_precision(amps, MED_PRECISION )
    float_as_thousands_str_with_precision(watts, MED_PRECISION )
    

    Then, if in the future you do something like this:

    HIGH_PRECISION = 6;
    MED_PRECISION = 4;
    LOW_PRECISION = 2;
    

    All you do is change the constants…

    But to try and answer the question in the OP title:

    IMO the only numbers that can truly be used and not be considered “magic” are -1, 0 and 1 when used in iteration, testing lengths and sizes and many mathematical operations. Some examples where using constants would actually obfuscate code:

    for (int i=0; i<someCollection.Length; i++) {...}
    
    if (someCollection.Length == 0) {...}
    
    if (someCollection.Length < 1) {...}
    
    int MyRidiculousSignReversalFunction(int i) {return i * -1;}
    

    Those are all pretty obvious examples. E.g. start and the first element and increment by one, testing to see whether a collection is empty and sign reversal… ridiculous but works as an example. Now replace all of the -1, 0 and 1 values with 2:

    for (int i=2; i<50; i+=2) {...}
    
    if (someCollection.Length == 2) {...}
    
    if (someCollection.Length < 2) {...}
    
    int MyRidiculousDoublinglFunction(int i) {return i * 2;}
    

    Now you have start asking yourself: Why am I starting iteration on the 3rd element and checking every other? And what’s so special about the number 50? What’s so special about a collection with two elements? the doubler example actually makes sense here but you can see that the non -1, 0, 1 values of 2 and 50 immediately become magic because there’s obviously something special in what they’re doing and we have no idea why.

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

Sidebar

Related Questions

I have function like this: function gi_insert() { if(!IS_AJAX){ $this->load->library('form_validation'); $this->form_validation->set_rules('name', 'Naslov', 'trim|required|strip_tags'); $this->form_validation->set_rules('body',
I have a function like this: MyFunction(double matrix[4][4]) {/*do stuff*/} I am calling this
I have a function like this in actionscript3 private function uploadFile(event:MouseEvent):void { var uploader:URLRequest
I have a function like this: def eventcateg_detail(request): ca = EventTypeCategory.objects.values() for i in
I have a function like this function login_redirect($redirect, $vars = array()) { return $redirect;
If I have a function like this: function abc($a,$b,$c = 'foo',$d = 'bar') {
Assuming I have a function like this my_method(const vector<const T*> & param); I wonder
I have a function defined like this: public static void ShowAbout(Point location, bool stripSystemAssemblies
I have a Datatable function like this public DataTable LoadCategory(SetupCategoryBO scBO) { DBConnect myConnection
Imagine I have a template function like this: template<typename Iterator> void myfunc(Iterator a, typename

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.