converting double to long

R

rkk

Hi,

I have to do something like below:

double d = 0.151177;
I need to have the mantissa part 151177 to a long variable i.e. I need
to write a function to do this conversion:

long convert(double value);

Please help as I am not able to precisely get this done using the C
code.

Regards
Kalyan
 
N

Nick Keighley

I have to do something like below:

double d = 0.151177;
I need to have the mantissa part 151177 to a long variable i.e. I need
to write a function to do this conversion:

long convert(double value);

Please help as I am not able to precisely get this done using the C
code.

does modf() do what you want?
 
I

Ike Naar

I have to do something like below:
double d = 0.151177;
I need to have the mantissa part 151177 to a long variable i.e. I need
to write a function to do this conversion:
long convert(double value);

It is not really clear what you want; can you give a better description
of what you're looking for, and perhaps a few examples?
E.g. what would be the required "mantissa part" of 0.0012435 ? Of 1234.5000 ?

Also note that, in general, a value like 0.151177 cannot be represented
as a double.
In one (common) floatingpoint format it would be stored as
0.15117700000000000581934500587522052228450775146484375
with mantissa 0.604708000000000023277380023500882089138031005859375
and exponent -2 .
 
S

s.dhilipkumar

Hi,

I have to do something like below:

double d = 0.151177;
I need to have the mantissa part 151177 to a long variable i.e. I need
to write a function to do this conversion:

long convert(double value);

Please help as I am not able to precisely get this done using the C
code.

Regards
Kalyan

Kalyan,

Do you want to take everything after '.' the decimal point and store
it in a long variable?

then one method (may be not that efficient) i can think of now is.

long convert(double value)
{
char value_str[124]={0};
int val_len=0;
long tenth=1,result=0;

val_len=strlen(sprintf(value_str,"%f%c",value,'\0');
while(value_str[val_len] != '.')
{
result+= value_str[val_len]-'0' * tenth;
tenth = (tenth == 1)?10:(tenth * 10);
}
return result;
}

ofcourse with the nessary error checks.
 
B

Ben Bacarisse

It's best to snip the parts you don't need for context or comment.
Do you want to take everything after '.' the decimal point and store
it in a long variable?

then one method (may be not that efficient) i can think of now is.

long convert(double value)
{
char value_str[124]={0};
int val_len=0;
long tenth=1,result=0;

val_len=strlen(sprintf(value_str,"%f%c",value,'\0');

sprintf adds a null -- you don't have to.
while(value_str[val_len] != '.')
{
result+= value_str[val_len]-'0' * tenth;

value_str[val_len] is (initially) the null added by sprintf.
subtracting '0' gives you a mysterious negative number. You have two
nulls so this will happen twice. Well, it would if you moved
val_len. as it is you just keep looking at the same character.
tenth = (tenth == 1)?10:(tenth * 10);

why not just tenth *= 10; ?
}
return result;
}

ofcourse with the nessary error checks.

I'd strtoul(strchr(value_str, '.'), NULL, 10) rather than doing it
myself. Use the library, Luke.
 
S

s.dhilipkumar

Ben,
Thanks for the comments, just typed in without even thinking twice.
My basic intension was to show a way for the poster to achive the
task. (looks like i have shown a bad way :) )

<btw Snniped the unwanted>

Now it is still ugly?

long convert(double value)
{
char value_str[124]={0};
int val_len=0;
long tenth=1,result=0;

val_len=strlen(sprintf(value_str,"%f",value));
while(value_str[--val_len] != '.')
{
result+= value_str[val_len]-'0' * tenth;
tenth *= 10;
}
return result;

}

and

"strtoul(strchr(value_str, '.'), NULL, 10)" is even better. Thanks.
 
M

mohangupta13

Ben,
   Thanks for the comments, just typed in without even thinking twice..
My basic intension was to show a way for the poster to achive the
task.  (looks like i have shown a bad way :) )

<btw Snniped the unwanted>

Now it is still ugly?

long convert(double value)
{
  char value_str[124]={0}; why 124 only????
  int val_len=0;
  long tenth=1,result=0;

  val_len=strlen(sprintf(value_str,"%f",value));
  while(value_str[--val_len] != '.')
  {
    result+= value_str[val_len]-'0' * tenth;
    tenth *= 10;
  }
  return result;

}

and

"strtoul(strchr(value_str, '.'), NULL, 10)" is even better.  Thanks.
 
M

mohangupta13

Hi,

I have to do something like below:

double d = 0.151177;
I need to have the mantissa part 151177 to a long variable i.e. I need
to write a function to do this conversion:

long convert(double value);

Please help as I am not able to precisely get this done using the C
code.

Regards
Kalyan

double round(double num,int places){
int val=(int)num;
double fract=num-val;

double fact_p=fract*pow(10,places);
int fact_p_val=(int)fact_p;
double fact_p_fact=fact_p-fact_p_val;

int digit=(int) (fact_p_fact*10);
if(digit>=5)
fact_p_val++;
return (val+ (double)fact_p_val/pow(10,places));



}
this seems to be working for all positive doubles' ...
Is it fine ?? Or am I missing something??
Mohan
 
M

mohangupta13

Ben,
   Thanks for the comments, just typed in without even thinking twice..
My basic intension was to show a way for the poster to achive the
task.  (looks like i have shown a bad way :) )

<btw Snniped the unwanted>

Now it is still ugly?

long convert(double value)
{
  char value_str[124]={0}; why only 124 ???
  int val_len=0;
  long tenth=1,result=0;

  val_len=strlen(sprintf(value_str,"%f",value));
  while(value_str[--val_len] != '.')
  {
    result+= value_str[val_len]-'0' * tenth;
    tenth *= 10;
  }
  return result;

}

and

"strtoul(strchr(value_str, '.'), NULL, 10)" is even better.  Thanks.
 
M

mohangupta13

Hi,

I have to do something like below:

double d = 0.151177;
I need to have the mantissa part 151177 to a long variable i.e. I need
to write a function to do this conversion:

long convert(double value);

Please help as I am not able to precisely get this done using the C
code.

Regards
Kalyan

double round(double num,int places){

int val=(int)num;
double fract=num-val;

double fact_p=fract*pow(10,places);
int fact_p_val=(int)fact_p;
double fact_p_fact=fact_p-fact_p_val;

int digit=(int) (fact_p_fact*10);
if(digit>=5)
fact_p_val++;
printf("\ndigit =%d ",digit);
return (val+ (double)fact_p_val/pow(10,places));



}
it works for positive values ...Is it fine or did i miss something??
Mohan
 
B

Ben Bacarisse

double round(double num,int places){

int val=(int)num;

This can overflow. modf is probably the best way to do this.
double fract=num-val;

double fact_p=fract*pow(10,places);

I would do the rounding here by adding a half (or doing something more
subtle if more careful rounding is needed).
int fact_p_val=(int)fact_p;

Again, overflow is possible. Another modf (this time for the
integer part) and a check that the result fits in a long and we are
done. I'd return -1L when the result does not fit.
 
M

mohangupta13

This can overflow.  modf is probably the best way to do this.



I would do the rounding here by adding a half (or doing something more
subtle if more careful rounding is needed).
can you explain this is more detail . as i get it you meant
..3456 +.5=.8456 right?? So how does it round up??
 
B

Ben Bacarisse

can you explain this is more detail . as i get it you meant
.3456 +.5=.8456 right?? So how does it round up??

It doesn't! I was thinking of something else -- not sure what now. I
should have said, "by adding one if the fractional part is a half or
more". You are about to do another modf to get the integer part so
you can add one based on the fractional return:

if (modf(fact_p, &integer_part) >= 0.5)
integer_part += 1.0;
return integer_part > LONG_MAX ? -1L : integer_part;

(I am assuming the function returns long since that is what the OP
wanted).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top