Here's a topic for my C questions:


Code:

int radium;
scanf("%f",&radium);
printf("%f",radium);


If I input 5 there, I get 0.00000 with the print.

Any idea? It needs to be float, I can't work with integers only here.
If you want a float as a input, why are you declaring your variable as a integer? int's and float's are not interchangeable like that.
(edit note:) If you have any other integer variables you are wishing to do math operations with the float, it would be best to make those variables floats as well because there is quite alot of overhead from swapping register types like that, when the code gets built into x86.
tribal wrote:
If you want a float as a input, why are you declaring your variable as a integer? int's and float's are not interchangeable like that.
Very correct, thanks for that.

Quote:
(edit note:) If you have any other integer variables you are wishing to do math operations with the float, it would be best to make those variables floats as well because there is quite alot of overhead from swapping register types like that, when the code gets built into x86.
Sooooort of, although not as much as you might think. Understanding the difference between floats and doubles is also important./
tribal wrote:
If you want a float as a input, why are you declaring your variable as a integer? int's and float's are not interchangeable like that.


Clearly you haven't seen this: (from the Quake 3 Arena source code)


Code:
float Q_rsqrt( float number )
{
        long i;
        float x2, y;
        const float threehalfs = 1.5F;
 
        x2 = number * 0.5F;
        y  = number;
        i  = * ( long * ) &y;                       // evil floating point bit level hacking
        i  = 0x5f3759df - ( i >> 1 );               // what the f*ck?
        y  = * ( float * ) &i;
        y  = y * ( threehalfs - ( x2 * y * y ) );   // 1st iteration
//    y  = y * ( threehalfs - ( x2 * y * y ) );   // 2nd iteration, this can be removed
 
        return y;
}


Razz

Quote:
(edit note:) If you have any other integer variables you are wishing to do math operations with the float, it would be best to make those variables floats as well because there is quite alot of overhead from swapping register types like that, when the code gets built into x86.


Not at all. Actually, swapping registers is one of the fastest things you can do. Loading from a regular register into an SSE register for super duper fast floating point is very quick. Hence why the code snippet above is so insanely awesome. Likewise, a common trick for super fast rounding is to add .5 and cast to an int. (eg, myint = (int) (myfloat + .5f))

The OP's bug is actually that floating point and ints have different bit-level structure and scanf doesn't know that the second parameter is actually an int and not a float like he specified. Changing it to scanf("%d") would work fine, or just using a float throughout.
Well yeah, that particular magic number doesn't really count. Razz I think what he was actually trying to say with regards to that last bit is that casting between integers and floating points is slow.
KermMartian wrote:
I think what he was actually trying to say with regards to that last bit is that casting between integers and floating points is slow.


I know what he meant, and casting between ints and floats is far from slow. "quite alot [sic] of overhead" is completely wrong, there almost no overhead, and what little there is is far from slow.
It was all a mater of distraction, thanks guys!

I have a new question, I have a few doubts with the theorical part of C.


Code:

char * str1 = "Cemetech is a cool forum";


What is that? A string, an array of chars, or a pointer?

Thanks.

EDIT:
I have problems with the practical part too now it seems:

Code:
#include <stdio.h>
#include <string.h>
#include <math.h>

int main()
{
    char * option;
    printf("1. Circle Area\n2. Sphere Volume\nChoose: ");

    gets(option);

    if (strcmp (option,"1") == 0)
    {
        float radium;
        scanf("%f",&radium);
        printf("%f",radium);
        printf("%d",circleArea(radium));
    }
    return 0;
}
float circleArea(float r)
{
    return r*r*M_PI;
}


I get 'conflicting types for circleArea' error.
Quote:
What is that? A string, an array of chars, or a pointer?
Yes. A C string is an array of characters. Arrays are sequences of data objects, like characters, and are pointers (the pointer that is the array points to the first item). char hello[3] is an array. hello is a pointer; hello[0] is *hello, hello[1] is *(hello+1), hello[2] is *(hello+2).

You should be prototyping circleArea:


Code:
#include <stdio.h>
#include <string.h>
#include <math.h>

int main(); //function prototypes
float circleArea(float r);

//program body
int main()
{
//stuff
}

float circleArea(float r)
{
    return r*r*M_PI;
}
I thought we only had to do that declaring thing in C++.

Thanks, here is my last program:


Code:
#include <stdio.h>

int main()
{
    printf("Enter an integer: ");
    int num;
    scanf("%d",&num);
    int n;
    int counter = 1;
    while (1)
    {
        printf("Enter an integer (or 0 to quit): ");
        scanf("%d",&n);
        if (n!=0)
        {
            num+=n;
            counter++;
        }
        else
        {
            break;
        }
    }
    printf("Average: %d",num/counter);
    return 0;
}


Coded it by hand last night and compiled it today, works perfectly Very Happy

I just made a new program! ;D


Code:
#include <stdio.h>
#include <string.h>
#include <math.h>

float circleArea(float r);
int main();
float sphereVolume(float r);

int main()
{
    printf("1. Circle Area\n2. Sphere Volume\nChoose: ");
    int option;
    scanf("%d",&option);

    if (option==1)
    {
        float radium;
        printf("Enter radium: ");
        scanf("%f",&radium);
        printf("%f",circleArea(radium));
    }
    if (option==2)
    {
        float radium;
        printf("Enter radium: ");
        scanf("%f",&radium);
        printf("%f",sphereVolume(radium));
    }
    return 0;
}
float sphereVolume(float r)
{
    return (4.f/3.f)*M_PI*r*r*r;
}
float circleArea(float r)
{
    return r*r*M_PI;
}
What does it think the average of 1 and 2 is? You may need to cast the operands of the division to floating-point numbers.
benryves wrote:
What does it think the average of 1 and 2 is? You may need to cast the operands of the division to floating-point numbers.
Did he change it since you posted? Because 4.f/3.f looks correct to me.
Scout changed the second program after prompting on IRC, though the first program still computes the average as num/counter where both num and counter are integers.
As benryves taught me:

3 is an int
3.f is a float
3.0 is a double

Very Happy
benryves wrote:
He changed the second program after prompting on IRC, though the first program still computes the average as num/counter where both num and counter are integers.
Ah, of course, I was looking at the second program. Scout, to further clarify what benryves said:

Code:
printf("Average: %d",num/counter);

to

Code:
printf("Average: %f",((float)num)/((float)counter));
Oh yeah, even if the input is always integers, averages can be float, but I didn't really test that program.

Thanks KermM.
ScoutDavid wrote:
Oh yeah, even if the input is always integers, averages can be float, but I didn't really test that program.

Thanks KermM.
Sure thing. By the way, I'm not convinced that 3.0 is always a double, benryves, do you have a source for that? Or is Scout misquoting you? Wink Scout, does the idea of casting make sense to you?
KermMartian wrote:
ScoutDavid wrote:
Oh yeah, even if the input is always integers, averages can be float, but I didn't really test that program.

Thanks KermM.
Sure thing. By the way, I'm not convinced that 3.0 is always a double, benryves, do you have a source for that? Or is Scout misquoting you? Wink Scout, does the idea of casting make sense to you?


No idea what casting is.

Regarding what benryves said, I'm pretty sure he said 3.0 is a double, but I might be wrong.
According to section 3.1.3.1 (floating constants) of this version of the ANSI C standard (I'm not sure where to find a better one, sorry):

Quote:
An unsuffixed floating constant has type double. If suffixed by the letter f or F, it has type float. If suffixed by the letter l or L, it has type long double.
Very good, I checked and I agree with that from my own compiler. Wink I haven't dealt with that low-level stuff lately, sadly. Sad Scout: casting is things like turning floats into doubles, doubles into floats, floats and doubles into ints, changing ints to and from unsigned ints, shorts, and all the other numerical types. It's done by prefixing the desired target type in parentheses, eg (float)myint.
KermMartian wrote:
Very good, I checked and I agree with that from my own compiler. Wink I haven't dealt with that low-level stuff lately, sadly. Sad Scout: casting is things like turning floats into doubles, doubles into floats, floats and doubles into ints, changing ints to and from unsigned ints, shorts, and all the other numerical types. It's done by prefixing the desired target type in parentheses, eg (float)myint.


CASTING, I now what that is Very Happy

As in:


Code:

print int("5") //Python


Just never used it yet in C. Thanks.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
» Goto page 1, 2, 3, 4, 5, 6, 7, 8, 9, 10  Next
» View previous topic :: View next topic  
Page 1 of 10
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement