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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:38:07+00:00 2026-05-16T04:38:07+00:00

noob here wants to calculate compound interest on iPhone. float principal; float rate; int

  • 0

noob here wants to calculate compound interest on iPhone.

float principal;
float rate;
int compoundPerYear;
int years;
float amount;

formula should be: amount = principal*(1+rate/compoundPerYear)^(rate*years)

I get slightly incorrect answer with:

amount = principal*pow((1+(rate/compoundPerYear)), (compoundPerYear*years));

I’m testing it with rate of .1, but debugger reports .100000001 .

Am I doing it wrong? Should I use doubles or special class (e.g., NSNumber)?

Thanks for any other ideas!


After further research it seems that the NSDecimalNumber class may be just what I need. Now I just have to figure out how to use this bad boy.

  • 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-16T04:38:08+00:00Added an answer on May 16, 2026 at 4:38 am

    double will get you closer, but you can’t represent 1/10 exactly in binary (using IEEE floating point notation, anyway).

    If you’re really interested, you can look at What Every Computer Scientist Should Know About Floating-Point Arithmetic. Link shamefully stolen from another SO thread.

    The quick and dirty explanation is that floating point is stored in binary with bits that represents fractional powers of 2 (1/2, 1/4, 1/8, …). There is simply no mathematical way to add up these fractions to exactly 1/10, thus 0.1 is not able to be exactly represented in IEEE floating point notation.

    double extends the accuracy of the number by giving you more numerals before/after the radix, but it does not change the format of the binary in a way that can compensate for this. You’ll just get the extra bit somewhere later down the line, most likely.

    See also:

    • Why can’t decimal numbers be represented exactly in binary?
    • What’s wrong with using == to compare floats in Java?

    and other similar threads.


    Further expansion that I mulled over on the drive home from work: one way you could conceivably handle this is by just representing all of the monetary values in cents (as an int), then converting to a dollars.cents format when displaying the data. This is actually pretty easy, too, since you can take advantage of integer division’s truncating when you convert:

    int interest, dollars, cents;
    interest = 16034; //$160.34, in cents
    dollars = value / 100; //The 34 gets truncated: dollars == 160
    cents = value % 100; //cents == 34
    printf("Interest earned to date: $%d.%d\n", dollars, cents);
    

    I don’t know Objective-C, but hopefully this C example makes sense, too. Again, this is just one way to handle it. It would also be improved by having a function that does the string formatting whenever you need to show the data.

    You can obviously come up with your own (even better!) way to do it, but maybe this will help get you started. If anyone else has suggestions on this one, I’d like to hear them, too!

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

Sidebar

Related Questions

Git noob here. I know this is against the principal of distributed source control
noob here still experimenting with templates. Trying to write a message processing class template
NOOb here. I've got a HTTP request that pulls all of the content from
Total noob here with javascript. I'm trying to alter a function. This is the
Total noob here. This is my first c# attempt, its a console application that
Doxygen noob here. I've searched and tried to resolve this for several hours now
Rails noob here. I have a rails application with (in this example) three tables.
c#/oop noob here. I am creating a datatable from the schema of a sql
Totally a PHP noob here so sorry for the possibly basic question - here
Hey! Total CakePHP noob here. Updated at bottom / This is sort of a

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.