Printing float values in C


 
Thread Tools Search this Thread
Top Forums Programming Printing float values in C
# 1  
Old 11-01-2012
Printing float values in C

Hi,

I have small problem to print float value in the fallowing code
Code:
float Cx, W,f=250000, Cr=92.00,pi=3.14;

W=2*pi*f;
Cx = 1/W.Cr;  //Cx value will be come around like 7.07E-9.
printf("capacitance value: %.10f",Cx);

I am trying to print Cx value using above code but it was not printing like that, the value i am getting when i tried to print Cx value using above code is 7.077140 . I am getting of 6 digit precision value only. How to get 10 digit precision value or how to print like this 7.07E-9.

Thanks in advance.
# 2  
Old 11-01-2012
Your program does not compile. What is "1/W.CR" supposed to be? That is not valid C.

Ever heard the old adage "measure with micrometer, mark with chalk, cut with axe"? Not even pi is accurate here, you're not going to get anything fantastic out.

You can force scientific notation with %e. From man 3 printf:

Code:
       e, E   The  double  argument  is  rounded  and  converted  in the style
              [-]d.ddde+-dd where there is one digit before the  decimal-point
              character and the number of digits after it is equal to the pre-
              cision; if the precision is missing, it is taken as  6;  if  the
              precision  is  zero,  no  decimal-point character appears.  An E
              conversion uses the letter E (rather than e)  to  introduce  the
              exponent.   The exponent always contains at least two digits; if
              the value is zero, the exponent is 00.

# 3  
Old 11-02-2012
Quote:
Originally Posted by veerubiji
I am getting of 6 digit precision value only. How to get 10 digit precision value
In addition to Corona688 reply, you should be aware that there is no way to get an accurate 10 digit precision with floats. You can only expect about 7 digit precision with them. You need to use double precision variables (~ 16 digit precision) instead.
# 4  
Old 11-23-2012
Isn't just this?:
Code:
printf("%lf", floatNumber);

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Array not printing values if used in a loop

Hello! I'm making an English to Morse Code translator and I was able to mostly get it all working by looking through older posts here; however, I have one small problem. When I run it it's just printing spaces for where the characters should be. It runs the right amount of times, and if I try... (3 Replies)
Discussion started by: arcoleman10
3 Replies

2. Shell Programming and Scripting

Printing $values using awk

Hi All I had requirement where I need to re-order columns in a file by using a control file. here is the ctrl file c1 c2 c3 source file c3 | c1 | c2 a | b| c I should create output file based on the ctrl file columns o/p should look like this c1 | c2 | c3 b| c|a I wrote some... (9 Replies)
Discussion started by: gvkumar25
9 Replies

3. Programming

Printing values from a class

I have a class and want to print values in MOD using L = new Layer* ; How can I print the values in MOD using this object L??? class Layer { public : Model* MODP; Model* MODS; (1 Reply)
Discussion started by: kristinu
1 Replies

4. Shell Programming and Scripting

printing two values with TAB in between

Dear friends, I want to print variables' values in certain format where space between two values of variables is "a tab" I tried where I provided "tab" between two varibales. But when it print values on screen its giving me output without spaces in two values. Request you to help me in... (7 Replies)
Discussion started by: anushree.a
7 Replies

5. UNIX for Advanced & Expert Users

Printing defaulted values

I have written a phyton script that accepts command line arguments. I am setting defaults for some of them if the user does not specify them. However I want to print the user values and the defaulted values seperately. However, once I set the default values, then I cannot use if... (0 Replies)
Discussion started by: kristinu
0 Replies

6. UNIX for Advanced & Expert Users

Get Minimun from a Float values

Hi Guys, here is a part of my source code. This part is reponsible to get the minimun of a few values: .......... MAX=0.00 for a in `cat $OFILE` do if then ... (2 Replies)
Discussion started by: Paat
2 Replies

7. Shell Programming and Scripting

SH if statement using FLOAT values

Today I spent longer than I'd like to admit figuring out how to write a Bourne shell IF statement that tests a FLOAT value before executing a block of statements. Here's the solution I found, which invokes bc. Hope this will come in handy for someone: value = testval = if then body... (5 Replies)
Discussion started by: sjepsen
5 Replies

8. Programming

math.h: float ceilf(float x)

Good morning, I'm testing the use of ceilf: /*Filename: str.c*/ #include <stdio.h> #include <math.h> int main (void) { float ceilf(float x); int dev=3, result=0; float tmp = 3.444f; printf("Result: %f\n",ceilf(tmp)); return 0; } (1 Reply)
Discussion started by: jonas.gabriel
1 Replies

9. UNIX for Dummies Questions & Answers

Dividing float values

Hi I know its a dumb question but can any one please explain me the difference of executing a shell script in the following 2 ways. . script.sh sh script.sh I have a problem if I execute the following code as sh script.sh DB_CNT_ALW=0.20 SCT_VAR=0.05 if ; then echo "== Difference... (3 Replies)
Discussion started by: shash
3 Replies

10. Shell Programming and Scripting

comparing two float values

I am trying to compare 2 float values using if the foll code does not work a=1.4 b=1.6 if test $a -gt $b then echo "$a is max" else echo "$b is max" fi does -gt work for floating point numbers, if not how do go about for my requirement? can i use bc ? pls help thanks in advance... (2 Replies)
Discussion started by: kavitha
2 Replies
Login or Register to Ask a Question