Click here to Skip to main content
16,020,347 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QuestionEight
{
    class Program
    {
              static void Main(string[] args)
        {
            const int N = 100;
            double sum = 0;
            for (int i = 1; i <= N; i++)
            {
                sum = sum +1.0/i ; // <-- change this to sum = sum+1/i;

            }
            Console.WriteLine("Sum ={0}", sum);

        }
    }
}
Posted
Comments
Sergey Alexandrovich Kryukov 3-Mar-14 19:22pm    
I guess, you are the beginner. Not to worry — learn the basics. It's very good that you are working with basics, not jumping to UI, databases or any "cool" stuff prematurely, like many other do. First get confident with basics. Keep it this way, focus on fundamentals...
—SA

1 solution

This is because you don't understand integer division and implicit type conversion; or you may miss the literal syntax. If you use "1" instead of "1.0", this is considered as integer, and, depending on int i value, 1/1 == 1, 1/2 == 0, 1/3 == 0, and so on. With "1", you get this integer values "rounded" to 0, and only later this zero is converted to double (floating-point) zero, which is added to sum.

With double, you get fractional values of floating-point arithmetic for 1/2, 1/3, etc. If this is what you need, you should use immediate constant as double literal, or convert it or i to double using typecast expression, such as (double)i. If you have mixed-type binary expression one operand is automatically casted to "wider" type, but better don't rely in it at first, better use the typed literal syntax explained below.

This is how you should really write double literal equal to double zero: 1d or 1.0d, but ".0" does not really matter as soon as you use "d". So, just use 1d.

—SA
 
Share this answer
 
v5
Comments
RalvarezHose 3-Mar-14 19:27pm    
Got it thanks. I was thinking, I declared the "sum" as double, therefore the output of sum will also be in decimals.
Sergey Alexandrovich Kryukov 3-Mar-14 19:43pm    
Not quite: don't mix up the different concepts: Decimal (type name), Double/Float and decimal (notation for representing numbers using 10-based system). Numbers cannot be hexadecimal or decimal, only string representing number can be. You don't work with strings (and try to avoid working with strings representing data instead of data itself, this is a big fallacy in many beginners these days). This is double, and should be double in all compatible expressions; mixing with other types presents the catch you just reported.

Your problem is now solved. Will you accept the answer formally (green "Accept" button)?

—SA
Matt T Heffron 3-Mar-14 19:54pm    
1.0 does NOT default to single precision (float)
The default for floating point constants is double.
Suffix with f (e.g., 1.0f) for float.

CSharp Language Specification section 2.4.4.3 Real literals
"If no real-type-suffix is specified, the type of the real literal is double."
Sergey Alexandrovich Kryukov 3-Mar-14 20:06pm    
Oops! I guess you know that; and I messed up things, will remove this statement.
Thank you very much for this note.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900