[gcalctool] Use superscript digits for scientific notation
- From: Robert Ancell <rancell src gnome org>
- To: svn-commits-list gnome org
- Cc:
- Subject: [gcalctool] Use superscript digits for scientific notation
- Date: Sun, 20 Dec 2009 07:21:56 +0000 (UTC)
commit d532626df46c4747cdf9410a99247e0a2d017744
Author: Robert Ancell <robert ancell gmail com>
Date: Sun Dec 20 18:21:10 2009 +1100
Use superscript digits for scientific notation
ChangeLog | 3 ++-
src/display.c | 12 ++++++++++--
src/gtk.c | 14 +++++++++++++-
src/mp-equation-lexer.l | 11 ++++++-----
src/mp-equation-parser.y | 6 +++---
src/mp-equation.c | 9 +++++++--
6 files changed, 41 insertions(+), 14 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 2ff94d3..a2dafd5 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -9,7 +9,8 @@ gcalctool change history.
2009-12-20 Robert Ancell <robert ancell gmail com>
- * Make 0^0 = 1 (Bug #605011)
+ * Make 0^0 = 1 (Robert Ancell, Bug #605011)
+ * Use superscript digits for scientific notation (Robert Ancell)
2009-12-17 Robert Ancell <robert ancell gmail com>
diff --git a/src/display.c b/src/display.c
index 07ebf63..94629be 100644
--- a/src/display.c
+++ b/src/display.c
@@ -615,10 +615,11 @@ void display_set_angle_unit(GCDisplay *display, MPAngleUnit angle_unit)
static void
make_eng_sci(GCDisplay *display, char *target, int target_len, const MPNumber *x, int base_)
{
- char fixed[MAX_DIGITS];
+ char fixed[MAX_DIGITS], *c;
MPNumber t, z, base, base3, base10, base10inv, mantissa;
int eng, exponent = 0;
GString *string;
+ const char *super_digits[] = {"�", "¹", "²", "³", "�", "�", "�", "�", "�", "�"};
string = g_string_sized_new(target_len);
@@ -661,7 +662,14 @@ make_eng_sci(GCDisplay *display, char *target, int target_len, const MPNumber *x
mp_cast_to_string(&mantissa, base_, display->accuracy, !display->show_zeroes, fixed, MAX_DIGITS);
g_string_append(string, fixed);
- g_string_append_printf(string, "Ã?10^%d", exponent);
+ g_string_append_printf(string, "Ã?10");
+ if (exponent < 0) {
+ exponent = -exponent;
+ g_string_append(string, "â?»");
+ }
+ snprintf(fixed, MAX_DIGITS, "%d", exponent);
+ for (c = fixed; *c; c++)
+ g_string_append(string, super_digits[*c - '0']);
strncpy(target, string->str, target_len);
g_string_free(string, TRUE);
diff --git a/src/gtk.c b/src/gtk.c
index b909690..bda826a 100644
--- a/src/gtk.c
+++ b/src/gtk.c
@@ -375,6 +375,14 @@ do_button(int function, gpointer arg)
GtkTextIter start, end;
gint cursor_start, cursor_end;
+ /* Disable super/subscript mode when finished entering */
+ if (function == FN_CALCULATE ||
+ function == FN_CLEAR ||
+ (function == FN_TEXT && strstr("â?»â?°Â¹Â²Â³â?´â?µâ?¶â?·â?¸â?¹â??â??â??â??â??â??â??â??â??â??", (char *)arg) == NULL)) {
+ gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(X.subscript_toggle), FALSE);
+ gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(X.superscript_toggle), FALSE);
+ }
+
if(gtk_text_buffer_get_selection_bounds(X.display_buffer, &start, &end)) {
cursor_start = gtk_text_iter_get_offset(&start);
cursor_end = gtk_text_iter_get_offset(&end);
@@ -1176,7 +1184,8 @@ main_window_key_press_cb(GtkWidget *widget, GdkEventKey *event)
do_text("µ");
return TRUE;
case GDK_e:
- do_text("Ã?10^");
+ do_text("Ã?10");
+ gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(X.superscript_toggle), TRUE);
return TRUE;
case GDK_f:
do_button(FN_FACTORIZE, NULL);
@@ -1196,6 +1205,9 @@ main_window_key_press_cb(GtkWidget *widget, GdkEventKey *event)
gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(X.superscript_toggle))) {
switch(event->keyval)
{
+ case GDK_minus:
+ do_text("â?»");
+ return TRUE;
case GDK_0:
do_text("â?°");
return TRUE;
diff --git a/src/mp-equation-lexer.l b/src/mp-equation-lexer.l
index c3f7318..d13f006 100644
--- a/src/mp-equation-lexer.l
+++ b/src/mp-equation-lexer.l
@@ -50,20 +50,21 @@ DECIMAL "."|","
DEC {ZERO}|{ONE}|{TWO}|{THREE}|{FOUR}|{FIVE}|{SIX}|{SEVEN}|{EIGHT}|{NINE}
HEX {DEC}|[A-F]
SUPER_DIGITS "�"|"¹"|"²"|"³"|"�"|"�"|"�"|"�"|"�"|"�"
+SUPER_MINUS "â?»"
SUB_DIGITS "â??"|"â??"|"â??"|"â??"|"â??"|"â??"|"â??"|"â??"|"â??"|"â??"
FRACTION "½"|"â??"|"â??"|"¼"|"¾"|"â??"|"â??"|"â??"|"â??"|"â??"|"â??"|"â??"|"â??"|"â??"|"â??"
-INVERSE "�¹"
GREEKS "α"|"β"|"γ"|"δ"|"ε"|"ζ"|"η"|"θ"|"ι"|"κ"|"λ"|"μ"|"ν"|"ξ"|"ο"|"Ï?"|"Ï?"|"Ï?"|"Ï?"|"Ï?"|"Ï?"|"Ï?"|"Ï?"|"Ï?"|"Ï?"
LETTERS [a-zA-Z]|{GREEKS}
SUP_NUM {SUPER_DIGITS}+
+NSUP_NUM {SUPER_MINUS}{SUPER_DIGITS}+
SUB_NUM {SUB_DIGITS}+
WORD {LETTERS}+
DEC_NUM {DEC}+|{DEC}*{DECIMAL}{DEC}+
BASE_NUM {HEX}+{SUB_NUM}|{HEX}*{DECIMAL}{HEX}+{SUB_NUM}
NUMBER {DEC_NUM}|{BASE_NUM}|{FRACTION}|{DEC_NUM}{FRACTION}
-VARIABLE {WORD}|{WORD}{SUB_NUM}|{WORD}{INVERSE}|{GREEKS}
+VARIABLE {WORD}|{WORD}{SUB_NUM}|{WORD}"�¹"|{GREEKS}
MOD [mM][oO][dD]
AND "â?§"|[aA][nN][dD]
@@ -88,10 +89,10 @@ IN [iI][nN]
{XOR} {return tXOR;}
{IN} {return tIN;}
{NUMBER} {if (mp_set_from_string(yytext, &yylval->int_t) != 0) REJECT; return tNUMBER;}
-{SUP_NUM} {yylval->integer = super_atoi(yytext); return tSUPNUM; }
-{SUB_NUM} {yylval->integer = sub_atoi(yytext); return tSUBNUM; }
+{SUP_NUM} {yylval->integer = super_atoi(yytext); return tSUPNUM;}
+{NSUP_NUM} {yylval->integer = super_atoi(yytext); return tNSUPNUM;}
+{SUB_NUM} {yylval->integer = sub_atoi(yytext); return tSUBNUM;}
{VARIABLE} {yylval->name = strdup(yytext); return tVARIABLE;}
-{INVERSE} {return tINVERSE;}
[ \t\n]
. {return *yytext;}
diff --git a/src/mp-equation-parser.y b/src/mp-equation-parser.y
index 5cb68ef..f5e7690 100644
--- a/src/mp-equation-parser.y
+++ b/src/mp-equation-parser.y
@@ -107,11 +107,11 @@ static void do_conversion(yyscan_t yyscanner, const MPNumber *x, const char *x_u
%left tNOT
%left tROOT tROOT3 tROOT4
%left <name> tVARIABLE
-%right <integer> tSUBNUM tSUPNUM
+%right <integer> tSUBNUM tSUPNUM tNSUPNUM
%left BOOLEAN_OPERATOR
%left PERCENTAGE
%left UNARY_MINUS
-%right '^' tINVERSE '!' '|'
+%right '^' '!' '|'
%left tIN
%type <int_t> exp variable
@@ -136,7 +136,7 @@ exp:
| '|' tNUMBER tVARIABLE '|' {get_variable(yyscanner, $3, &$$); mp_multiply(&$2, &$$, &$$); mp_abs(&$$, &$$); free($3);} /* FIXME: Shouldn't need this rule but doesn't parse without it... */
| exp '^' exp {mp_xpowy(&$1, &$3, &$$);}
| exp tSUPNUM {mp_xpowy_integer(&$1, $2, &$$);}
-| exp tINVERSE {mp_reciprocal(&$1, &$$);}
+| exp tNSUPNUM {mp_xpowy_integer(&$1, $2, &$$);}
| exp '!' {mp_factorial(&$1, &$$);}
| variable {mp_set_from_mp(&$1, &$$);}
| tNUMBER variable %prec MULTIPLICATION {mp_multiply(&$1, &$2, &$$);}
diff --git a/src/mp-equation.c b/src/mp-equation.c
index 814a5ba..831dee6 100644
--- a/src/mp-equation.c
+++ b/src/mp-equation.c
@@ -120,9 +120,14 @@ sub_atoi(const char *data)
int
super_atoi(const char *data)
{
- int i, value = 0;
+ int i, sign = 1, value = 0;
const char *digits[11] = {"�", "¹", "²", "³", "�", "�", "�", "�", "�", "�", NULL};
+ if(strncmp(data, "â?»", strlen("â?»")) == 0) {
+ sign = -1;
+ data += strlen("â?»");
+ }
+
do {
for(i = 0; digits[i] != NULL && strncmp(data, digits[i], strlen(digits[i])) != 0; i++);
if(digits[i] == NULL)
@@ -131,7 +136,7 @@ super_atoi(const char *data)
data += strlen(digits[i]);
} while(*data != '\0');
- return value;
+ return sign * value;
}
static int
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]