How to print huge decimal number in C? [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have a variable that stores huge value like 0.11111121323532534 but I dont know how to print it as a whole, I found the use of printf('%.30g', variable) but dont know how it works. Is it the only way to output this variable and why it works

The default floating point precision for printing purposes in C is six digits. That's the equivalent of:
printf("%.6f", variable)
In order to have more than 6 digits displayed after the decimal point you have to format the number as you have done. However, you used the "%g" format type which returns the shorter of the float (%f) or the scientific notation (%e) format.
On top of that, a double may only accurately represent up until ~15/16 numbers in total, regardless of the decimal point, being 8bytes in allocated size (with 1 bit for the sign, 11 for the exponent and 52 for your mantissa), leaving you able to mostly represent numbers smaller than 2^53.
printf("%.15f", variable)
The above will work as well, and will allow up to 15 digits to be displayed after the decimal point.

Related

printf ("%d", 0.0625) prints a zero while nearby values such as 0.0624 print absurd numbers as you would expect, why is 0.0625 different? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I also noticed that multiples of 0.0625 such as 1.25 keep giving zero but I was unable to find an explanation
as per CostantinoGrana
"Do you know how IEEE 754 works? The numbers which you say "give 0"
are multiples of a not too negative negative power of 2. So when you
store them in little endian, the 32 less significant bits, that are
all 0, are the first thing you find in memory as int. Your int is 32
bits wide, thus 0."

Handling decimals in C without float [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have a problem in C, I am not allowed to use floats as the microcontroller it will be flashed does not support that data type. Now all my integers are being rounded off as it should. How do I handle this case?
A short research indicates using bit wise operation such as left shift and right shift. I know what are these operations. But I do not know how to use these operations to achieve what I want.
Another possibility is the Q number format.
You will get some results if you google "Q number format" or some variations.
It is often used for some DSP related topics in C. Here another blog post that explains that number format and here is an example code implementation for q-numbers in C.
In general you can say that q-numbers represent a number between -1 and 1 without using floating point arithmetic.
Normally a microcontroller don't have a floating point unit, everything works with integers. But its up to you which unit you like for your integers.
For example:
100 could be 100 cm or 1,00 m
1000 could be 100,0 cm or 1,000 m and so on..
Please have a look at the description:
electronic.stackexchange

Which datatype is used for 10^500 in c language [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Problem Statement
Addition is a very basic operation in mathematics. Jimmy was very weak in addition, so his father decided to teach him. Jimmy is given a number and has to perform addition on all the digits of that number till that the large number gets converted into a single digit. Your task is to prepare a program for him so that he can easily find out the final number.
Input Format
First line contains T (1<=T<=100) the number of test cases.
Each test case contains integer N (1<=N<=10^100).
Output Format
For each test case, output the one digit number by repeatedly adding the digits.
Constraints
1<=T<=100
1<=N<=10^500
I'd represent the very large input number as char, and the total of the digits (first pass) will easily fit in an int. You'll need a little more than simple arithmetic, but it shouldn't be difficult (case seems a likely way to manage the job).

Are there ways to specify the conversion character for double and float in C without having to know the number of its decimal? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
In C I have learned that to specify a conversion character for float or double I will have to use %.xf. Is it possible to not specify and display double or float without having to count up the number of its decimal.
EDIT:Sorry if I am not understanding this because I am just a beginner at programming in general, and C is my first language.
double reallyBigPi = 3.1415914159141591415914159;
printf("Big Pi = %f\n\n", reallyBigPi);
My goal is to print out this input but using the suggested %f I was only able to get Big Pi = 3.141591. So in the end if I want the get that amount I will have to count up the decmial point?
Sadly, neither the C nor the C++ standard libraries expose an interface to Dragon4 (*) (or an improved algorithm) which would correctly determine the number of fractional decimal numbers from a binary floating point representing a decimal number. The libraries are required (by IEEE 754) to correctly format the values, though.
Obviously, determining the number of fractional digits is not equivalent to omitting the precision. Simply omitting the precision behaves the same as specifying the default precision (which, I think, is 6).
You may be able to use Double Conversion as that does expose an interface determining the number of decimals. I found Double Conversion relatively hard to use, though.
(*) Dragon4 is an algorithm described in "How to print floating-point numbers accurately". Note that this link is to an ACM site which asks for payment for the article. I don't have a link to a [legal] free source of the paper.

printf incorrect output for doubles [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I compute two numbers using a CPU and a GPU. They are both double precision floating point numbers.
When I print them using printf I get:
CPU=11220648352037704534577864266910298593595406193008158076631751727790009102214012225556499863999825039525639767460911091800702910643896210872459798230329601182926117099298535084878987264.00000 GPU=-4.65287
using:
void print(const double *data1, const double *data2) {
...
printf("CPU=%.5f\tGPU=%.5f\n", data1[k], data2[k]);
}
Which is way to many digits I would expect. Why do I get this? Do I overflow, underflow, corrupt memory? Please help.
Thanks.
You used the printf format %.5f. That means "print plain decimal digits all the way down to five places after the decimal." If you want scientific notation instead, which is more common with such large numbers, you should use %.5g, which means "print automatic format digits all the way down to five places after the decimal...or really four places in scientific notation."
Note that such huge numbers as you have are definitely within range for a double. There is nothing unusual about the value of the number in the code you have posted.

Resources