double trouble

Bj

Addon Developer
Addon Developer
Donator
Joined
Oct 16, 2007
Messages
1,886
Reaction score
11
Points
0
Location
USA-WA
Website
www.orbiter-forum.com
using
Code:
double
in c im getting weird things happening like;

Code:
double sum(double x, double y);
void run()
{
     double x,y;
     x = 5;
     y = 3;
     printf("The sum of 5 and 3 is %d\n",sum(x,y);
}
double sum(double x, double y)
{
     return x+y;
}
*note: I didnot build this to see if it actually works ;) so just get the jist of it for now, :lol:

for something like the example above, it will return some wierd number like 42,554,654,860:blink:

But when I use int instead of double then it comes out allright, :huh:

Problem with compiler???
 
  • Like
Reactions: Tex

Bj

Addon Developer
Addon Developer
Donator
Joined
Oct 16, 2007
Messages
1,886
Reaction score
11
Points
0
Location
USA-WA
Website
www.orbiter-forum.com
I figured out what I did wrong, the compiler was making double as unsigned, not signed, so in binary, thats like

0-00000000000000000000000000000001=11111111111111111111111111111111
and 32 1's in binary is a huge number. :lol:
like 2,147,483,647
Code:
double sum(double x, double y);
void run()
{
     double x,y;
     x = 5;
     y = 3;
     printf("The sum of 5 and 3 is %d\n",sum(x,y);
}
double sum(double x, double y)
{
     return x+y;
}
 

Bj

Addon Developer
Addon Developer
Donator
Joined
Oct 16, 2007
Messages
1,886
Reaction score
11
Points
0
Location
USA-WA
Website
www.orbiter-forum.com

I just found out that doubles are automaticlaly signed, so that means that that wasnot the problem,
:huh:
I still should run the compiler for the above code, just to see if that actually works,
 

computerex

Addon Developer
Addon Developer
Joined
Oct 16, 2007
Messages
1,282
Reaction score
17
Points
0
Location
Florida
Your problem is you are not using the printf function correctly. The "%d" place holder is only for integer data type! Try this:

Code:
int sum (int x, int y)
{
    return x + y;
}

int main()
{
    /*Lets add 5 and 3 */
    int answer = sum(5,3);
    /*lets now print it to the screen */
    printf("%d + %d = %d", 5, 3, answer);
    return 0;
}

BTW, this is the add-on development forum...Shouldn't this be moved to off-topic ?
 

Bj

Addon Developer
Addon Developer
Donator
Joined
Oct 16, 2007
Messages
1,886
Reaction score
11
Points
0
Location
USA-WA
Website
www.orbiter-forum.com
Well that should work for ints, but what about decimals?
double is used for decimals, so what if we had the sum of like
35.2345
and
67.23456

I looked it up in my book, isn't it supposed to be( %i )correct?

so just moding your code;
Code:
double sum (double x, double y)
{
    return x + y;
}

int main()
{
    int answer = sum(4.456,43.4354);
    printf("4.456 + 43.4354 = %i", answer);
    return 0;
}
oh, and yes it should be in addon development forum because I got my compiler to work with orbiter, except, I actually want to do something to it! :)

like for example (starting small)

using the sample-sdk it gave a starter mfd, on this mfd how do I go about printing HI!

(again starting small) ;)
:lol:
 

computerex

Addon Developer
Addon Developer
Joined
Oct 16, 2007
Messages
1,282
Reaction score
17
Points
0
Location
Florida
For decimals, you use floating point variables. You can use doubles as well, but use the %f place holder. I don't do much c programming, so I haven't used printf much, but I believe %i is another place holder for int type. Try this:
Code:
double sum (double x, double y)
{
    return x + y;
}

int main()
{
    double answer = sum(4.456,43.4354);
    printf("4.456 + 43.4354 = %.4f", answer);
    return 0;
}


Orbiter is programmed in C++, not c. I have never used the printf function in coding for Orbiter, and you most likely won't be either. You use GDI to print stuff to the screen in Orbiter, not c functions that print to the STD.
I suggest that instead of learning C, you get a book on C++ and learn that instead, otherwise it can be confusing later to move from a procedural language to an object oriented language.
 

Bj

Addon Developer
Addon Developer
Donator
Joined
Oct 16, 2007
Messages
1,886
Reaction score
11
Points
0
Location
USA-WA
Website
www.orbiter-forum.com
For decimals, you use floating point variables. You can use doubles as well, but use the %f place holder. I don't do much c programming, so I haven't used printf much, but I believe %i is another place holder for int type. Try this:
Code:
double sum (double x, double y)
{
    return x + y;
}

int main()
{
    double answer = sum(4.456,43.4354);
    printf("4.456 + 43.4354 = %.4f", answer);
    return 0;
}
Orbiter is programmed in C++, not c. I have never used the printf function in coding for Orbiter, and you most likely won't be either. You use GDI to print stuff to the screen in Orbiter, not c functions that print to the STD.
I suggest that instead of learning C, you get a book on C++ and learn that instead, otherwise it can be confusing later to move from a procedural language to an object oriented language.

you said %.4f dont you mean, %f ?

oh, and thanks for the advice on c++, I will try to buy a book from walmart or something.
 

computerex

Addon Developer
Addon Developer
Joined
Oct 16, 2007
Messages
1,282
Reaction score
17
Points
0
Location
Florida
%.4f will print 4 places after the decimal point. With %f you get a lot of 0s at the end of the digit.
 
Top