Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,100 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,672 people online right now. Registration is fast and FREE... Join Now!




Using precision to specify how many numbers after decimal point

 
Reply to this topicStart new topic

Using precision to specify how many numbers after decimal point, How do I avoid shorter numbers...

Daikide
2 Oct, 2007 - 09:07 AM
Post #1

New D.I.C Head
*

Joined: 20 Aug, 2007
Posts: 16


My Contributions
I need average for my program. And I'm using it multiple time in my program.

I'm doing this in PVSS:

So say average float is 2.0013005 (FOR EXAMPLE) but the number in the end can be higher... so my program takes this average and gives me 2.0013 in the log file. After it uses this shorter version for other calculations.instead of my original 2.0013005 (It cuts it and well that should NOT be a problem, but because of the arithmetics I use. This gap adds on with each datapoint and then it results in the slight difference in the end.

MY QUESTION IS, DO YOU KNOW HOW CAN I PREVENT MY PROGRAM FORM GIVING ME A SHORTER NUMBER INSTEAD OF A FULL ONE?

CODE

main ()

{    

    string s, path_name;
    file f;  
    int  year1, month1, day1, hour1, min1, sec1, i, N;   // value MOVED to dyn_int
    dyn_float A; //  
    time  ts, TS1, TS2;
    float MD, SD, average, setting, value, sum;

path_name = path_nameG.text;

s = TS1_panel.text;
sscanf (s, "%d.%d.%d %d:%d:%d", year1, month1, day1, hour1, min1, sec1);
TS1 = makeTime (year1, month1, day1, hour1, min1, sec1);


s = TS2_panel.text;
sscanf (s, "%d.%d.%d %d:%d:%d", year1, month1, day1, hour1, min1, sec1);
TS2 = makeTime (year1, month1, day1, hour1, min1, sec1);

//Button.enabled = FALSE;

f = fopen (path_name, "r"); // Have to read in help. parameters here.  

    
       //fgets (f, s);  

      
      
      
  while (feof(f)==0) {  ///////// (f) --------- in brackets changed to path_name.

      fgets (s, 100, f); // INT COUNT MUST BE PROVIDED!  

      sscanf (s, "%d.%d.%d %d:%d:%d %f %f", year1, month1, day1, hour1, min1, sec1, setting, value); /// SHOULD BE "f" INSTEAD OF "s"

      //DebugN (year1, month1, day1, hour1, min1, sec1, setting, value);

      ts = makeTime (year1, month1, day1, hour1, min1, sec1);

       //TS1 - Initial timestamp
      // TS2 - Final timestamp


      if ((ts >= TS1) && (ts <= TS2)) {          
           // DebugN (ts);
            dynAppend (A, value); //////////// VALUE!!!!!!!!!!!!!!!!111111111 is used in sscanf TOO!
       }
  }

   int N = dynlen (A);

         sum = 0;

  for (int i = 1; i <= N; i++)

{
  sum += A[i];
  // sum = sum + A[i]
  }

average = sum/N;  // AVERAGE    
DebugN (average);


// Standard Deviation ..........................................

  for (int i = 1; i <= N; i++)  {
  //SD=0; //DELE!
     SD += (A[i] - average)*(A[i] - average);
      //sum += A[i];
  
    }

SD = SD/N;
    
SD = sqrt(SD);

TV.text (SD);

// if ((TS2 < TS1) || (TS2 = TS1))  {   // Time Stamp Comparsion  TEMP!

//    TV.text (" Wrong TS input");
  
    
    
// return 0;  
//}
  
  
  
   }


This post has been edited by jayman9: 2 Oct, 2007 - 12:55 PM
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Using Precision To Specify How Many Numbers After Decimal Point
2 Oct, 2007 - 11:32 AM
Post #2

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
there are builtin limitations to how "long" a number can be - meaning how much precision the datatype can handle. if you are having problems with data truncation like this, the usual first step is to move to a datatype with more precision. in this case, you are using a float, which has the least precision of all of the floating point number implementations. try using double or long double instead.

and in cases where you are outputting to a file, you can also change the precision tag in the printf statement - this won't give you any more precision than the data type can hold, but it will allow you to change from the default display precision.

-jjh
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 08:53PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month