Page 1 of 1

Creating calculator programme. Looping variables

Posted: Sun Nov 29, 2009 5:30 pm
by jcvamp
I'm creating a calculator to run on the Nintendo DS, but I've found that the variables loop when they reach a certain value. Is there a way of preventing this?

Also, I'm using double precision floating point numbers (double), so that I can have numbers after the decimal point displayed, but I don't know how to make it so that the amount of numbers displayed after the point is limited to the amount there are (eg. not '1.0' or '2.542000').

Code: Select all

double num=0;
and

Code: Select all

PA_OutputText(1, 0, 0,"%f0",num);
I've read that by changing the number after f you can specify the amount of decimal places to show, but when the amount of decimal places will vary, I don't know how to make that work.

Thanks.

Re: Creating calculator programme. Looping variables

Posted: Mon Nov 30, 2009 3:09 am
by Normmatt
just use %f and it will automatically round it to the nearest decimal.

Re: Creating calculator programme. Looping variables

Posted: Mon Nov 30, 2009 9:14 am
by vuurrobin
also know that floating point numbers on the ds are slow because the ds doesn't have a floating point unit. try to avoid using them.

Re: Creating calculator programme. Looping variables

Posted: Mon Nov 30, 2009 2:16 pm
by elhobbs
vuurrobin wrote:also know that floating point numbers on the ds are slow because the ds doesn't have a floating point unit. try to avoid using them.
I doubt this is going to matter for a calculator.

Re: Creating calculator programme. Looping variables

Posted: Mon Nov 30, 2009 3:54 pm
by jcvamp
Thanks for the responses.
Normmatt wrote:just use %f and it will automatically round it to the nearest decimal.
When I do that it outputs the numbers left of the decimal point followed by '.ÿC'.

Re: Creating calculator programme. Looping variables

Posted: Tue Dec 01, 2009 5:20 pm
by RyouArashi
Use %lf for doubles (=64bit). %f is for floats (=32bit).

Re: Creating calculator programme. Looping variables

Posted: Thu Dec 03, 2009 7:44 pm
by jcvamp
RyouArashi wrote:Use %lf for doubles (=64bit). %f is for floats (=32bit).
Thanks.

Re: Creating calculator programme. Looping variables

Posted: Fri Dec 04, 2009 5:04 pm
by WinterMute
jcvamp wrote:Thanks for the responses.
Normmatt wrote:just use %f and it will automatically round it to the nearest decimal.
When I do that it outputs the numbers left of the decimal point followed by '.ÿC'.
This makes me suspect that you're running into an issue with the PAlib output functions.

i'm not entirely sure what Normmatt means by nearest decimal tbh - the default precision for %f seems to be 6 decimal places but you can change that by explicitly changing the precision in the format string. "%.8f" will use 8 decimal places.

You can position your string in tile co-ordinates using the libnds console using the appropriate escape sequence. "\x1b[line;columnH". Here's some code to illustrate.

Code: Select all

#include <nds.h>
#include <string.h>
#include <stdio.h>

//---------------------------------------------------------------------------------
int main(int argc, char **argv) {
//---------------------------------------------------------------------------------

	consoleDemoInit();

	float temp = 22.0f/7.0f;
	
	printf("%f\n",temp);
	printf("%.8f\n",temp);
	printf("%.16f\n",temp);
	printf("\x1b[23;0H%.16f",temp);

	while(1) swiWaitForVBlank();

}

Re: Creating calculator programme. Looping variables

Posted: Tue Dec 08, 2009 12:33 am
by jcvamp
Thanks for the response. What I want to happen is for any extra zeros to be cut off. Your demonstration shows a lot of different precision levels, but none of them display the number the way I want it to. I want it to be dynamic. I want '12' to display as '12' and '1.6622' to display as '1.6622' without adding extra zeros to '12' (ie, '12.0000').

Re: Creating calculator programme. Looping variables

Posted: Tue Dec 08, 2009 7:10 pm
by Izhido
Well, you can always do that manually... use sprintf() to print to a char[] with %.4f, then traverse the new string backwards replacing any '0' with \0 (until, of course, next char is not '0' anymore), then print the string using printf.