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)

You can read it as a string

firstPerson , if you can - give couple of code lines for example)

In reality, you could probably use some clever bit-wise tricks and some low-level instructions to quickly extract this kind of information (which is probably what library code does). But that's too complicated to do in an exercise question.

Assuming you will not go as far as outputting the result in a "smart" format (like scientific notation when appropriate and so on) and that you are only printing numbers reasonably close to 0 (between 10^6 and 10^-6 or so), then you can simply use an algorithm roughly like this. First, you can have an algorithm to print an integer value:

  • Find the lowest power N of 10 that is greater than you number (M).
  • Then, for each power of ten, call it i, between 10^N and 1:

    • print the character corresponding to the integer division M / i
    • subtract the higher digit from the number, as so: M = M - i * (M / i);.

Now that you have an algorithm to print an integer, printing a double value is simply a matter of splitting it into the "integer" value that comes before the decimal place and the "integer" value that comes after. Here is a simple way to do that:

int get_before_decimal_pt(double value) {
  if( value >= 0.0 )
    return int(std::floor(value));
  else
    return int(std::ceil(value));
};

int get_after_decimal_pt(double value, int number_of_digits) {
  value = value - get_before_decimal_pt(value);
  return int(std::fabs(value) * std::pow(10,number_of_digits));
};

Then, you get two integers which you can print. First, you print the before-decimal-point integer. Then, you print a dot character. And finally, you print the after-decimal-point integer (remember to modify the print function such that you print all the leading 0 characters, that is, if you have number_of_digits == 4 and the after-decimal-point integer comes out as 42, then you should print 0042).

That is about as detailed instructions as can be given for this problem.

I would want an example from you since your description of the problem makes no sense to me. Show is what you are trying to accomplish and your attempt at the code to accomplish it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.