Problem with displaying zero in a matrix

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • crazygrey
    New Member
    • Nov 2007
    • 7

    Problem with displaying zero in a matrix

    Hi,
    I have created a program that multiply matrices, but the problem has to do with not showing 0, instead showing 1.795e-09 or some other smaller numbers.
    One of the matrices I get for example :
    Matrix=[1.795e-09,-1,0,-2.962e-10,
    1,1.795e-09,8.034e-36,0.1]
    I don't want the elements to be integers, at least floats (I have defined the contents to be double). But, how do you get rid of this problem?

    thanks
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    Originally posted by crazygrey
    Hi,
    I have created a program that multiply matrices, but the problem has to do with not showing 0, instead showing 1.795e-09 or some other smaller numbers.
    One of the matrices I get for example :
    Matrix=[1.795e-09,-1,0,-2.962e-10,
    1,1.795e-09,8.034e-36,0.1]
    I don't want the elements to be integers, at least floats (I have defined the contents to be double). But, how do you get rid of this problem?

    thanks
    I think u are using a matrix which stored double values and using a wrong format specifier(like %f instead of %lf ) to print it. I am not sure about the format specifier for double.

    Raghuram

    Comment

    • crazygrey
      New Member
      • Nov 2007
      • 7

      #3
      I haven't specified anything for the double format. If you check the format specifiers here %f is specified for float data.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Originally posted by crazygrey
        I haven't specified anything for the double format. If you check the format specifiers here %f is specified for float data.
        The format specifier %f is for a float and %lf is for a double.

        You may bee dealing with uninitialized variables. I can't see your code so I am just guessing here.

        Comment

        • crazygrey
          New Member
          • Nov 2007
          • 7

          #5
          Originally posted by weaknessforcats
          The format specifier %f is for a float and %lf is for a double.

          You may bee dealing with uninitialized variables. I can't see your code so I am just guessing here.
          The variables are initialized. The elements of the matrix are defined to be "double". As my understanding there are no literals for double number (1.456 for e.g.) but for float (1.456f). Anyways, I tried doing
          double sum;
          sum=0%lf;
          but I ran the code, I got this error (" %lf undeclared identifier ")

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Originally posted by crazygrey
            Anyways, I tried doing
            double sum;
            sum=0%lf;
            but I ran the code, I got this error (" %lf undeclared identifier ")
            The %lf is a format specifier for functions like printf(), scanf(), etc.
            [code=c]
            printf("%lf", sum);
            [/code]

            Comment

            • crazygrey
              New Member
              • Nov 2007
              • 7

              #7
              Originally posted by weaknessforcats
              The %lf is a format specifier for functions like printf(), scanf(), etc.
              [code=c]
              printf("%lf", sum);
              [/code]
              Brilliant, thanks very much. This definitely solved my problem.

              Comment

              Working...