Creating calculator programme. Looping variables

Post Reply
jcvamp
Posts: 6
Joined: Sat Nov 21, 2009 7:55 pm

Creating calculator programme. Looping variables

Post by jcvamp » Sun Nov 29, 2009 5:30 pm

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.

Normmatt
Posts: 3
Joined: Fri Nov 27, 2009 11:10 am

Re: Creating calculator programme. Looping variables

Post by Normmatt » Mon Nov 30, 2009 3:09 am

just use %f and it will automatically round it to the nearest decimal.

User avatar
vuurrobin
Posts: 219
Joined: Fri Jul 11, 2008 8:49 pm
Location: The Netherlands
Contact:

Re: Creating calculator programme. Looping variables

Post by vuurrobin » Mon Nov 30, 2009 9:14 am

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.

elhobbs
Posts: 358
Joined: Thu Jul 02, 2009 1:19 pm

Re: Creating calculator programme. Looping variables

Post by elhobbs » Mon Nov 30, 2009 2:16 pm

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.

jcvamp
Posts: 6
Joined: Sat Nov 21, 2009 7:55 pm

Re: Creating calculator programme. Looping variables

Post by jcvamp » Mon Nov 30, 2009 3:54 pm

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'.

RyouArashi
Posts: 29
Joined: Sun Mar 29, 2009 9:23 pm

Re: Creating calculator programme. Looping variables

Post by RyouArashi » Tue Dec 01, 2009 5:20 pm

Use %lf for doubles (=64bit). %f is for floats (=32bit).

jcvamp
Posts: 6
Joined: Sat Nov 21, 2009 7:55 pm

Re: Creating calculator programme. Looping variables

Post by jcvamp » Thu Dec 03, 2009 7:44 pm

RyouArashi wrote:Use %lf for doubles (=64bit). %f is for floats (=32bit).
Thanks.

WinterMute
Site Admin
Posts: 2003
Joined: Tue Aug 09, 2005 3:21 am
Location: UK
Contact:

Re: Creating calculator programme. Looping variables

Post by WinterMute » Fri Dec 04, 2009 5:04 pm

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();

}
Help keep devkitPro toolchains free, Donate today

Personal Blog

jcvamp
Posts: 6
Joined: Sat Nov 21, 2009 7:55 pm

Re: Creating calculator programme. Looping variables

Post by jcvamp » Tue Dec 08, 2009 12:33 am

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').

User avatar
Izhido
Posts: 107
Joined: Fri Oct 19, 2007 10:57 pm
Location: Costa Rica
Contact:

Re: Creating calculator programme. Looping variables

Post by Izhido » Tue Dec 08, 2009 7:10 pm

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.

Post Reply

Who is online

Users browsing this forum: No registered users and 9 guests