~ubuntu-branches/ubuntu/precise/gcalctool/precise-proposed

« back to all changes in this revision

Viewing changes to src/mp-serializer.c

  • Committer: Bazaar Package Importer
  • Author(s): Robert Ancell
  • Date: 2011-02-01 14:37:04 UTC
  • mfrom: (1.3.17 upstream)
  • Revision ID: james.westby@ubuntu.com-20110201143704-ipo3nv351hrcfrlf
Tags: 5.91.6-0ubuntu1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
34
34
    PROP_BASE,
35
35
};
36
36
 
37
 
static GType number_format_type;
38
 
 
39
37
struct MpSerializerPrivate
40
38
{
41
39
    gint accuracy;            /* Number of digits to show */
223
221
}
224
222
 
225
223
 
226
 
static gchar *
227
 
mp_cast_to_exponential_string(MpSerializer *serializer, const MPNumber *x, gboolean eng_format)
 
224
static int
 
225
mp_cast_to_exponential_string_real(MpSerializer *serializer, const MPNumber *x, GString *string, gboolean eng_format)
228
226
{
229
 
    gchar *fixed, *c;
 
227
    gchar *fixed;
230
228
    MPNumber t, z, base, base3, base10, base10inv, mantissa;
231
229
    int exponent = 0;
232
 
    GString *string;
233
 
    gchar *result;
234
 
    const gchar *super_digits[] = {"⁰", "¹", "²", "³", "⁴", "⁵", "⁶", "⁷", "⁸", "⁹"};
235
 
 
236
 
    string = g_string_sized_new(1024);
237
230
 
238
231
    mp_abs(x, &z);
239
232
    if (mp_is_negative(x))
273
266
    fixed = mp_cast_to_string(serializer, &mantissa);
274
267
    g_string_append(string, fixed);
275
268
    g_free(fixed);
276
 
    if (exponent != 0) {
277
 
        gchar *super_value;
278
 
 
279
 
        g_string_append_printf(string, "×10"); // FIXME: Use the current base
280
 
        if (exponent < 0) {
281
 
            exponent = -exponent;
282
 
            g_string_append(string, "⁻");
283
 
        }
284
 
 
285
 
        super_value = g_strdup_printf("%d", exponent);
286
 
        for (c = super_value; *c; c++)
287
 
            g_string_append(string, super_digits[*c - '0']);
288
 
        g_free (super_value);
 
269
  
 
270
    return exponent;
 
271
}
 
272
 
 
273
 
 
274
static void
 
275
append_exponent(GString *string, int exponent)
 
276
{
 
277
    const gchar *super_digits[] = {"⁰", "¹", "²", "³", "⁴", "⁵", "⁶", "⁷", "⁸", "⁹"};
 
278
    gchar *super_value, *c;
 
279
  
 
280
    if (exponent == 0)
 
281
        return;
 
282
 
 
283
    g_string_append_printf(string, "×10"); // FIXME: Use the current base
 
284
    if (exponent < 0) {
 
285
        exponent = -exponent;
 
286
        g_string_append(string, "⁻");
 
287
    }
 
288
 
 
289
    super_value = g_strdup_printf("%d", exponent);
 
290
    for (c = super_value; *c; c++)
 
291
        g_string_append(string, super_digits[*c - '0']);
 
292
    g_free (super_value);
 
293
}
 
294
 
 
295
 
 
296
static gchar *
 
297
mp_cast_to_exponential_string(MpSerializer *serializer, const MPNumber *x, gboolean eng_format)
 
298
{
 
299
    GString *string;
 
300
    MPNumber x_real;
 
301
    gchar *result;
 
302
    int exponent;
 
303
 
 
304
    string = g_string_sized_new(1024);
 
305
 
 
306
    mp_real_component(x, &x_real);
 
307
    exponent = mp_cast_to_exponential_string_real(serializer, &x_real, string, eng_format);
 
308
    append_exponent(string, exponent);
 
309
 
 
310
    if (mp_is_complex(x)) {
 
311
        GString *s;
 
312
        gboolean force_sign = TRUE;
 
313
        MPNumber x_im;
 
314
 
 
315
        mp_imaginary_component(x, &x_im);
 
316
 
 
317
        if (strcmp(string->str, "0") == 0) {
 
318
            g_string_assign(string, "");
 
319
            force_sign = FALSE;
 
320
        }
 
321
 
 
322
        s = g_string_sized_new(1024);
 
323
        exponent = mp_cast_to_exponential_string_real(serializer, &x_im, s, eng_format);
 
324
        if (strcmp(s->str, "0") == 0 || strcmp(s->str, "+0") == 0 || strcmp(s->str, "−0") == 0) {
 
325
            /* Ignore */
 
326
        }
 
327
        else if (strcmp(s->str, "1") == 0) {
 
328
            g_string_append(string, "i");
 
329
        }
 
330
        else if (strcmp(s->str, "+1") == 0) {
 
331
            g_string_append(string, "+i");
 
332
        }
 
333
        else if (strcmp(s->str, "−1") == 0) {
 
334
            g_string_append(string, "−i");
 
335
        }
 
336
        else {
 
337
            if (strcmp(s->str, "+0") == 0)
 
338
                g_string_append(string, "+");
 
339
            else if (strcmp(s->str, "0") != 0)
 
340
                g_string_append(string, s->str);
 
341
 
 
342
            g_string_append(string, "i");
 
343
        }
 
344
        g_string_free(s, TRUE);
 
345
        append_exponent(string, exponent);
289
346
    }
290
347
 
291
348
    result = g_strndup(string->str, string->len + 1);
314
371
            g_free(s0);
315
372
            return s1;
316
373
        }
317
 
      break;
 
374
        break;
318
375
    case MP_DISPLAY_FORMAT_FIXED:
319
376
        return mp_cast_to_string(serializer, x);
320
377
    case MP_DISPLAY_FORMAT_SCIENTIFIC:
496
553
 
497
554
    g_type_class_add_private(klass, sizeof(MpSerializerPrivate));
498
555
 
499
 
    number_format_type = math_mp_display_format_get_type();
500
 
 
501
556
    g_object_class_install_property(object_class,
502
557
                                    PROP_SHOW_THOUSANDS_SEPARATORS,
503
558
                                    g_param_spec_boolean("show-thousands-separators",
517
572
                                    g_param_spec_enum("number-format",
518
573
                                                      "number-format",
519
574
                                                      "Display format",
520
 
                                                      number_format_type,
 
575
                                                      math_mp_display_format_get_type(),
521
576
                                                      MP_DISPLAY_FORMAT_AUTOMATIC,
522
577
                                                      G_PARAM_READWRITE));
523
578
    g_object_class_install_property(object_class,