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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:55:23+00:00 2026-05-13T20:55:23+00:00

Two quick questions if I may, is this how I should go about taking

  • 0

Two quick questions if I may, is this how I should go about taking two NSNumber objects, performing a calculation and ending up with a result that is also an NSNumber?

NSNumber *flux = [[NSNumber alloc] initWithDouble:100.0];
NSNumber *mass = [[NSNumber alloc] initWithDouble:3];

double intermediate = [flux doubleValue] / [mass doubleValue];
NSLog(@"INTER : %.20f", intermediate);

NSNumber *result = [[NSNumber alloc] initWithDouble:intermediate];
NSLog(@"RESULT: %@", result);

...
...

[flux release];
[mass release];
[result release];

Also looking at the results in console from NSLog, is there any loss of precision? I would assume there is not and what I am seeing is just display precision, but just curious?

INTER : 33.33333333333333570181
RESULT: 33.33333333333334

gary

  • 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-13T20:55:23+00:00Added an answer on May 13, 2026 at 8:55 pm

    (Tangential to your question but related)

    NSNumber isn’t intended to do base-10 math with. It’s largely there to wrap and store numerical values. If you need to do real math, you want to use a NSDecimal.

    NSDecimalNumber, an immutable subclass
    of NSNumber, provides an
    object-oriented wrapper for doing
    base-10 arithmetic. An instance can
    represent any number that can be
    expressed as mantissa x 10^exponent
    where mantissa is a decimal integer up
    to 38 digits long, and exponent is an
    integer from –128 through 127

    Despite the fact that we call them “computers” our logic engines can’t do actual math so they have to fake it. When you get to the extremes of very large or very small magnitude numbers, that faking begins to show. That is why you need custom numerical classes that can hold more information than just a string of digits.

    So, if you have any concerns about precision, use NSDecimal instead of NSNumber. NSDecimal is designed to perform precise calculations.

    Edit01:

    … how I should go about taking two
    NSNumber objects, performing a
    calculation and ending up with a
    result that is also an NSNumber?

    Strictly speaking, you should not use NSNumber for calculations. You will notice that NSNumber has no dedicated methods for doing math. You have to convert to scalar and then back again to an object. This causes a loss of precision and the precision can change depending on the hardware or the definitions of the scalars.

    NSDecimal by contrast can precisely represent very precise numbers because it holds them abstractly. It has dedicated methods for performing precise mathematical operations.

    Also looking at the results in console
    from NSLog, is there any loss of
    precision?

    Yes, there is a loss of mathematical precision beyond just the formatting. Scalars have different precision depending on their type and size of the number they store.. At large magnitudes, this causes problems with precision. If you mix types, say a NSInteger and a NSUInteger, you get the maximal precision of the NSInteger.

    You also run into all the old problems of using scalars.

    If you ask an NSNumber object for its
    value using a type that cannot hold
    the value, you get back an erroneous
    result—for example, if you ask for the
    float value of a number created with a
    double that is greater than FLT_MAX,
    or the integer value of a number
    created with a float that is greater
    than the maximum value of NSInteger.

    NSDecimal frees you from all these possible sources of error. It does precise mathematical calculations up to magnitudes way beyond what anyone would use in the real world.

    I repeat: If precision is a concern, don’t use NSNumber or scalar.

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

Sidebar

Related Questions

Two questions: Can someone point me to unbiased data that compares .NET performance to
Two quick questions: What would be a RegEx string for three letters and two
Quick background info: of the three database tables that are relevant for this question,
Two Questions: Will I get different sequences of numbers for every seed I put
Given two integers a and b , how would I go about calculating the
Can two domain objects show on the same page, when the list method is
This is continuing questions from Selecting the highest salary Assuming a table ' wagetable
Hey guys, quick question. I have a table of messages. There are two types
This is just a quick question before I dive deeper into converting my current
Quick text-processing question. It's not necessarily related to programming, but this is the best

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.