NO

Author Topic: double (/float) type character by character  (Read 4898 times)

vedro-compota

  • Guest
double (/float) type character by character
« on: May 02, 2012, 12:20:40 PM »
hi there)
my brothers-programmers) please tell me - how to get double type character by character - i need make custom realization of fprintf - so it's necessary for me to show double type numder on the screen.

(For integer type this problem could be solved by subtracting 32 - and after that putchar() can be used.)

big thanks in advance)

CommonTater

  • Guest
Re: double (/float) type character by character
« Reply #1 on: May 02, 2012, 04:08:52 PM »
Use sprintf(string,"%f",variable)  to print it to a string then you can do what you want with it.

See the Pelles C help file for more details...

vedro-compota

  • Guest
Re: double (/float) type character by character
« Reply #2 on: May 02, 2012, 08:31:02 PM »
I can use only language kernel - e.g. only putchar for the screen (without libraries)))

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: double (/float) type character by character
« Reply #3 on: May 02, 2012, 08:42:04 PM »
I can use only language kernel - e.g. only putchar for the screen (without libraries)))
Maybe you should explain a bit more in detail what you are trying to do, so we understand what possible constraints you have (due to homework/course assignment?)...

Ralf

vedro-compota

  • Guest
Re: double (/float) type character by character
« Reply #4 on: May 02, 2012, 08:49:55 PM »
yes - it's something like homework - i need to realize the fprintf()  - the base function for this  - as i understand is putchar() -
putchar() is enough for string type  =
Code: [Select]
%sand, as i think for int to
Code: [Select]
%i
« Last Edit: May 02, 2012, 09:07:51 PM by vedro-compota »

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: double (/float) type character by character
« Reply #5 on: May 02, 2012, 09:12:13 PM »
yes - it's something like homework - i need to realize the fprintf()  - the base function for this  - as i understand is putchar() -
putchar() is enough for string type  =
Code: [Select]
%sand, as i think for int to
Code: [Select]
%i
Well, as a general rule (which I am sure you have been told already), we do not do any homework for you...  ;)

Beside that, doing it generally for floats isn't that much different that doing it for integers. You just have to to separate the whole number part from the fraction and run a loop for each of the digits to be output, with possible padding in front of the whole number part and filling with zeros possibly on the fraction part, up to a defined precision...

Ralf

vedro-compota

  • Guest
Re: double (/float) type character by character
« Reply #6 on: May 03, 2012, 07:11:15 AM »
Quote
>>>>we do not do any homework for you...
i didn't asked you for this)
yes - I needed something  like this idea)thank you )
but this -
Quote
>>>>padding in front of the whole number part
I can't understand - could you give example (not code - just float number)

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2096
Re: double (/float) type character by character
« Reply #7 on: May 03, 2012, 09:07:31 AM »
Take a look to http://www.koders.com/ to have a look to printf or sprintf subroutines.
It is better to be hated for what you are than to be loved for what you are not. - Andre Gide

vedro-compota

  • Guest
Re: double (/float) type character by character
« Reply #8 on: May 03, 2012, 01:27:56 PM »
thanks for advice, frankie)
there's a source code here, but the solution is quite massive...

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2096
Re: double (/float) type character by character
« Reply #9 on: May 03, 2012, 03:22:50 PM »
I think that this http://www.koders.com/c/fid8684644B8325F0D91A103EA7750A9269E1F99856.aspx?s=ftoa#L651 is enough for you (the only ftoa function).
It is better to be hated for what you are than to be loved for what you are not. - Andre Gide