0

Hello,

I have a relatively straight through program where I collect some integer data and pass it to an external file. The problem that I have is that what I really need to do is to pass that value divided by another integer (255 to be more exact). I tried a couple of things such as:

fprintf(pFile,"%d/255 ",State[STATE_SONAR_0+i]);

or

fprintf(pFile,"%d ",State[STATE_SONAR_0+i]/255);

but that is NOT giving me the right output (the value of State[STATE_SONAR_0] divided by 255). Any idea of how I can implement it? Below is my complete program. Thank you.

r.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "Nclient.h"

void turn_front_sonar_on(void)
{
  int sn_order[16] = {0,2,4,6,8,10,12,14};
  conf_sn(50, sn_order);
  conf_tm(15); 
}

/*
 * main
 */
main(void)
{
  FILE *pFile;
  int i;
  connect_robot(1, MODEL_SCOUT2, "/dev/ttyS0", 38400);
  pFile = fopen ("log_in.txt","a");  

  /* turn on the front sonars */
  turn_front_sonar_on();

  /* the loop */

  while(1)
  {
    gs();
    i = 0;
    do 
    { 
     fprintf(pFile,"%d ",State[STATE_SONAR_0+i]);
     i=i+2;
    } while (i < 16);

    fprintf(pFile,"%d ",State[STATE_VEL_TRANS]);
    fprintf(pFile,"%d\n",State[STATE_VEL_STEER]);
    fprintf(pFile,"_D: ");

  }

  return(0);
}
5
Contributors
8
Replies
9
Views
9 Years
Discussion Span
Last Post by jephthah
0

fprintf(pFile,"%d ",State[STATE_SONAR_0+i]/255); That would be the syntactically correct way of do it.
However if the result of the division is a decimal number an int ("%d") will loose precision.
Moreover you are not checking the return of fprintf to ensure that it did the job. A negative number would indicate that there was failure.

0

when I tried the syntax

fprintf(pFile,"%d ",State[STATE_SONAR_0+i]/255);

I get an output of '0'. any idea what else I could try? thanks.

r.

0

when I tried the syntax

fprintf(pFile,"%d ",State[STATE_SONAR_0+i]/255);

I get an output of '0'. any idea what else I could try? thanks.

r.

As I said before; "%d" will loose precision if the result is a real number.
e.g. If the result is 0.1234, guess what is going to be printed to pFile?
Change "%d" to "%f".

0

State[STATE_SONAR] returns an integer in the range 0-255
State[STATE_VEL_TRANS] and State[STATE_VEL_STEER] both return an integer in the range -255 through 255.

what I need is the output with that number divided by 255 (I am training a neural network and it accepts inputs in the range of 0 through 1).

when I changed the "%d " to "%f " I'm getting a constant value of 2.079086 for all the variables, even though they all vary.

is there any other way that I can rewrite my program as to get the output that I'm looking for? Thanks.

r.

0

you might try format precision specifiers if you want to reduce the number of decimal places. for instance, "%.2f" will round it to two decimal places, dropping any trailing zeros in the fractional part.

1234.56
24.68
0.9

even better, "%8.03f" will round it to three decimal places, keep any trailing zeros in the fractional part, and keep the entire number right-justified within a constant 8-character-width field

1234.556
  24.680
   0.900

you could also do scientific exponent format if the values are particularly small or large. For instance, "%.3e" will print (up to) three significant decimal values in exponential format, dropping any trailing zeros

1.234e3
9.86e-9
...etc...

and you can likewise use zero-padding and/or field width placeholders as described above.

--http://www.cplusplus.com/reference/clibrary/cstdio/printf.html

.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.