Stop printf from padding exponent [duplicate] - c

This question already has answers here:
How to control the number of exponent digits after 'e' in C printf %e?
(3 answers)
Closed 3 years ago.
When I use this:
printf("%e", 2000.0);
I get output 2.000000e+003 which is correct but very ugly. I then tried like this:
printf("%.1e", 2000.0);
Which outputs 2.0e+003 which is far better. The question is how to remove the 2 padding zeroes in the exponent so that the output is 2.0e+3. Is there a specific printf specifier for that or do I have to write a function manually that analyzes the floating point number and prints it the way I want (I'd hate to have to do that)?

The standard C features for printf do not provide any means of controlling the number of digits in the exponent. If you want a different format, you will have to write your own code or obtain code from somewhere else.
(The %a conversion does use only as many digits in the exponent as needed, but it prints the value with hexadecimal.)

Related

Can i print int in binary format? [duplicate]

This question already has answers here:
Is there a printf converter to print in binary format?
(57 answers)
Closed 2 years ago.
Can I print an integer in binary without additional cycles, conditions, and so on? I know that you can print a number in HEX printf("%x", a) in decimal printf("%d", a) in octal printf("%o", a), but i can't find args for binary. Can you help me?
12 -> 00001100
First of all, no, printf doesn't provide a formater to print a integer in binary.
Second, do not get fooled. Using printf adds additional cycles. Converting an integer to a string (displaying it as decimal, hexadecimal, binary or other) takes some operations. In the case of binary, it's just that you have to do the conversion for printf and provide it the string containing the binary representation.

C Programming: Does Modulus Ignore Leading Zeros in Result ? How Can I Stop It? [duplicate]

This question already has answers here:
Printing leading 0's in C
(11 answers)
keeping leading zeros in C [duplicate]
(3 answers)
Closed 5 years ago.
I'm programming a microcontroller in C and since the compiler or chip doesn't handle Floating Point numbers very well, it is recommended to scale the floating point value up enough to get rid of the decimal and then when outputting the number, scale it back down with a proportional division and use modulus to find and display the fractional part. Here's an example:
Start with number 25.0625. Multiply by 10000 to get 250625. Good now we don't have a float anymore. Now to display this we use a line like this:
sprintf(buffer, "%li.%i", Temp / 10000, abs(Temp % 10000));
So we first get the 25 back out by / 10000, then we get the 0625 out by % 10000. Here's the problem though: It appears my modulus result ignores the leading zero in 0625 so when it concatenates the number back together, it becomes 25.625 instead of 25.0625. I can't count on there always being a leading zero, so stuffing a zero in there isn't the answer, and there could at other times be several leading zeros. My question is, how do I suppress the modulus from excluding leading zeros ? Thanks.
You can tell sprintf to format the number in a 4-digit field with leading zeroes.
sprintf(buffer, "%li.%04d", Temp / 10000, abs(Temp % 10000));
See Printing leading 0's in C?

C: strtof wrong conversion by a unit [duplicate]

This question already has answers here:
Why are floating point numbers inaccurate?
(5 answers)
Why is scanf taking the wrong input for large float numbers? [duplicate]
(1 answer)
Is floating point math broken?
(31 answers)
Closed 5 years ago.
I'm having problems with strtof conversions, some values it works well but another ones it returns +1 or -1 of the value. My program reads the value from a file but for simplicity I have tried putting directly the value to strtof and it has the same behavior.
My simple code:
#include <stdlib.h>
int main(int argc, char **argv)
{
printf("%f", strtof("17309217",NULL));
}
It returns
17309216.000000
The same value that it gives if I convert "17309216" or "17309215".
I have been doing some more tests and it seems a "pattern", "17309214" works well but "17309213", "17309212" and "17309211" all return "17309212". After this "17309210" works well but "17309209", "17309208" and "17309207" return "17309208", and so on...
What is the explanation for this behaviour? Now I convert the string with other methods, I don't need a method to convert it. I only want to know why strtof is doing this, so I can decide where I can use it and where can't.
I have been reading some other questions asked related to wrong conversions but all are related to the decimal rounding, not an entire unit.
Thanks in advance,
You're converting to a 32-bit floating point which may not have the precision to handle that many digits.

Floating point number is rounding off in C [duplicate]

This question already has answers here:
Why are floating point numbers inaccurate?
(5 answers)
Closed 6 years ago.
i started learning c. Today, while i am working on a program, i found an interesting thing and i made another small program (similar to my issue) to check it.
#include<stdio.h>
int main(void)
{
float num1=867.0;
float num2=.6921;
printf("sum = %.4f \n",num1+num2);
return 0;
}
if i run the above program, i am getting 867.6921 as the answer. but if i changed the .4%f to %f the answer is changing to sum = 867.692078. why's the change in the output? Also, is there any way that i can get the answer without using the .4%f?
but if i changed the .4%f to %f the answer is changing to sum = 867.692078
printf rounds the result to that many digits after the floating point you specified, the default is .6. While rounding, it uses the current floating point rounding mode, the default is round to the nearest.
The float type has about seven digits of precision (can be anywhere from 6-9 digits). One alternative is to use g instead of f. This fixes the number of precise digits in the number, not just those after the decimal.
printf("sum = %.7g \n",num1+num2);
This produces output
sum = 867.6921
and will always give seven digits of precision for any input. Number formats would be like :
0.000000E+07
0000000
000000.0
00000.00
0000.000
000.0000
...etc

int i=00100 error in TC++ for C programing Language [duplicate]

This question already has answers here:
Meaning of leading zero in integer literal
(3 answers)
Closed 8 years ago.
I am an amature C programer. I can only use C Programing language.
I have a following code containing a loop in TC++IDE.
It is simple code for printing consecutiveNo. till given value,
it contains something like this:
i = 00100
in the above line when i enter 00100 the colour of normal integer value changes.(It changes to dark blue/Navy blue)
And when i use this in my loop. instead of repeating 100 times it repeats only "64" times.
Same happens with any value which is like 023 instead 0f 23.
Please explain what kind of IDENTIFIER/Variable is 00100 or values similar to it are.
And Also explain Why does it happen so ? (64 instead of 100).
Regards and Thank You in advanced !
This happens because a numeric literal starting with a zero is interpreted as a number written in octal.
A numeric literal beginning with 0 is interpreted as octal number in C and as 100 in octal is 64 in decimal this explains what you observe.

Resources