• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Calculating e^x Simple Program Problem

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
EDIT: Problem resolved. Thanks for the help!
 
Saloon Keeper
Posts: 14501
325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Alex, welcome to CodeRanch!

First off, I suggest you UseCodeTags. This will make it more appealing for people to answer your queries.

A very quick analysis suggests that you should edit your loop so that the numerator and denominator are set as follows:

or shorthand:

I also recommend changing i into an int.
 
Marshal
Posts: 76869
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I remember having to learn that expression in Maths when I was about sixteen. It is actually
The first term is x^0/0! and the second term is x^1/1!
I have added code tags as Stephan suggested and you can see how much better it is to read.

You need to keep track of the division; eventually it will become vanishingly small, or you get ∞, in both of which cases you will have to stop your iteration
 
Campbell Ritchie
Marshal
Posts: 76869
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you are supposed to pass different values for n to the myexp() method, and the same value of x. The 'e'and 'f' parts are similar, but you pass different values of x.
 
Stephan van Hulst
Saloon Keeper
Posts: 14501
325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I don't really think he needs to keep track of the division. He just iterates n times, each iteration should make the value more precise.

Or I'm confused about what you're trying to point out :P
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alex, welcome to JavaRanch.

Please note that it's not good forum etiquette to delete your complete post once your question is answered.

One of the purposes of a forum is as a source of answers for people who are searching later on. If you delete the whole question, then the whole post will not be useful for people who have a similar problem later and find this post by searching.

So, please do not delete your original question when you've got your answer.
 
Campbell Ritchie
Marshal
Posts: 76869
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote: . . .. He just iterates n times, each iteration should make the value more precise. . . .

I realised that, but not until after I had posted
 
Tick check! Okay, I guess that was just an itch. Oh wait! Just a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic