This week's book giveaway is in the Artificial Intelligence and Machine Learning forum.
We're giving away four copies of Succeeding with AI and have Veljko Krunic on-line!
See this thread for details.
Win a copy of Succeeding with AI this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

WAP to find out the series 1 + 1/2! + 1/3! + ......... + 1/n! (n! = Factorial)

 
Ranch Hand
Posts: 106
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I am trying to find out the solution for the series 1 + 1/2! + 1/3! + ......... + 1/n! (n! = Factorial)! But when I store 1/factorial of 5 or suppose 6 in a variable the value of the variable is 0.0 but I should be 0.16666666666. So there lie the problem. I dry ran
my program and could not find any problem in my logic but in line 31 the problem is rising! The data type of the variable is double and it should store decimal values but! Please help!
 
Bartender
Posts: 10777
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ranajoy Saha wrote:I dry ran my program and could not find any problem in my logic but in line 31 the problem is rising! The data type of the variable is double and it should store decimal values but! Please help!


Yes, but the problem is actually occurring at line 30.

Q: What is the result of 1/factorial? (Tip: look at the types carefully).

Winston
 
Ranajoy Saha
Ranch Hand
Posts: 106
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Winston! Thank you very much! I just changed the data type of factorial variable and the work was done! But I need an explanation. when the data type of factorial variable is int and when I change line 30 to series_calc =(double) (1/factorial); then why isn't the same answer coming as previous one. (The answer is 1.0) I am confused on this part! Dont be angry on me if my question seems silly 'cause I am in school and I have just stated using Java language!
 
lowercase baba
Posts: 12838
52
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1 is an int. factorial is an int.

When you divide an int by an int, you get an int.
 
Winston Gutkowski
Bartender
Posts: 10777
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ranajoy Saha wrote:I just changed the data type of factorial variable and the work was done! But I need an explanation. when the data type of factorial variable is int and when I change line 30 to series_calc =(double) (1/factorial); then why isn't the same answer coming as previous one. (The answer is 1.0) I am confused on this part! Dont be angry on me if my question seems silly 'cause I am in school and I have just stated using Java language!


No probs. The answer is basically as fred said, so the result of 1/factorial is an int. Adding the cast as you have done simply the changes the type of that result; it doesn't change its value.

BTW, you didn't need to change the type of factorial. What do you think the result of 1.0/factorial is?

Winston
 
Ranajoy Saha
Ranch Hand
Posts: 106
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did change 1 to 1.0 while I was trying to find a answer to the query. The answer was correct! Thanks all for solving my problem. A big thanks to all!
 
Rancher
Posts: 1041
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is problematic too as factorial is of type int, and the factorial function grows pretty fast, so it can overgrow the integer range quickly.





2 2
3 6
4 24
5 120
6 720
7 5040
8 40320
9 362880
10 3628800
11 39916800
12 479001600
13 1932053504
14 1278945280
15 2004310016
16 2004189184
17 -288522240
18 -898433024
19 109641728
 
Rancher
Posts: 43015
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can try to improve matters by using long instead of int, but that doesn't go much further. You may want to resort to using the BigInteger class.
 
Marshal
Posts: 69019
275
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about ?
 
My favorite is a chocolate cupcake with white frosting and tiny ad sprinkles.
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic