Question about floating point variables

Pages: 12
@jRaskell,
Having said what I said, here's an example of one use of floating point numbers:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <stdio.h>
#include <math.h>
#include <string.h>

int main(int argc, char** argv) {
    float a  = 0.0, b  = 0.0, c  = 0.0, x1 = 0.0, x2 = 0.0;
    int   verbose = 1;
    char ch = 0;
        
    /* Get values to use: */
    printf("Enter a, b and c, space separated, followed by ENTER.\n"
           "Currently only integer numbers work.\n\n:"
          );

    scanf("%f %f %f", &a, &b, &c);
    fflush(stdin);
        
    printf("\nVerbose output? [y/N]: ");
       
    if ((ch = getchar()) == 'Y' || ch == 'y')
        verbose = 1;
    else
        verbose = 0;

	putchar('\n');
    
    if (verbose == 1) {
	    /* Print lots of output */
        printf("a = %f\nb = %f\nc = %f\n\n"
               "-b  = %f\nb^2 =  %f\n4ac =  %f\n2a  =  %f\n\n",
               a, b, c,
               -b, b * b, 4.0 * a * c, 2.0 * a
              );
              
        printf("x = -b +- sqrt(b^2 - 4ac)\n"
               "    =====================\n"
               "               2a\n\n"
               "x = (%f + sqrt(%f - %f)) / %f OR\n"
               "x = (%f - sqrt(%f - %f)) / %f\n\n",
               -b, b * b, 4.0 * a * c, 2.0 * a,
               -b, b * b, 4.0 * a * c, 2.0 * a
              );
    }
    
	/* The calculations are very simple using the quadratic formula. */
    x1 = (-b + sqrt((b * b) - (4 * a * c))) / (2 * a);
    x2 = (-b - sqrt((b * b) - (4 * a * c))) / (2 * a);
	/* Probably a computer would be faster at factorizing, or completing the square
	 * but this is a simple example, not a super-optimized production-quality
	 * microsoft program (ha!) </ms bashing>.
	 */
    
    printf("x = %f, %f\n", x1, x2);
    
    fflush(stdout);

    return 0;
}

It's a very, very quick C implementation of a quadratic equation solver. It works, but it's very simple and the code is quite... rushed. Anyway, yes, floating point is useful, but I stand by what I said: use it wisely, and avoid it if you don't need it (you do need it for quadratic equations).
Last edited on
That doesn't prove anything. The code above works fine for both integer and floating point values when given sane inputs. The problem is that the code above doesn't handle very many sane cases. (And it all works with FP values whether or not you input integers!)

For one thing, you assume too much of the sqrt() function.

For another, don't fflush(stdin);!

Sorry. :-\
It's not trying to prove anything. It's me saying "I agree, floating point can be useful, here is an example of why I was wrong earlier."

Anyway, it wasn't meant to be robust... just make my homework quicker.
Last edited on
hey guys keep discussing this topic.. i'm learning from you. maybe link me to an article about floating point.. BTW there is a tutorial from Stanford university in youtube that discusses how floating point is represented in binary..

but i am thinking if doubles are not capable of storing big numbers, is there anyway of doing this. maybe implement your own..
maybe implement your own..

You'd have to write your own compiler, or write in assembly language.
You'd have to write your own compiler
Um... Why? As far as I know, GMP is not a compiler implementation.
Last edited on
Uhhh... Am I misinterpreting what he was asking? I thought he was asking how you would make your own data types, like having a "long long double long float long" or something. But not just by typedefs; I thought he meant actually having the storage on the stack set aside for the a long long double long float long too.

What did he actually mean (as I'm obviously missing something here)?
He was asking about how to implement an arbitrary precision floating point type.
Oh. K. My mistake, then :l
Topic archived. No new replies allowed.
Pages: 12