comparison dmd/expression/Util.d @ 144:ea6325d0edd9

+ RealExp.toCBuffer
author Eldar Insafutdinov <e.insafutdinov@gmail.com>
date Tue, 14 Sep 2010 22:39:29 +0100
parents af1bebfd96a4
children 0c8cc2a10f99
comparison
equal deleted inserted replaced
143:95b3ed3cddd5 144:ea6325d0edd9
69 69
70 import std.stdio : writef; 70 import std.stdio : writef;
71 71
72 import core.stdc.math; 72 import core.stdc.math;
73 import core.stdc.string; 73 import core.stdc.string;
74 import core.stdc.stdlib;
74 75
75 /*********************************** 76 /***********************************
76 * Utility to build a function call out of this reference and argument. 77 * Utility to build a function call out of this reference and argument.
77 */ 78 */
78 Expression build_overload(Loc loc, Scope sc, Expression ethis, Expression earg, Identifier id) 79 Expression build_overload(Loc loc, Scope sc, Expression ethis, Expression earg, Identifier id)
1769 /* In some cases, the REALPAD bytes get garbage in them, 1770 /* In some cases, the REALPAD bytes get garbage in them,
1770 * so be sure and ignore them. 1771 * so be sure and ignore them.
1771 */ 1772 */
1772 memcmp(&x1, &x2, REALSIZE - REALPAD) == 0; 1773 memcmp(&x1, &x2, REALSIZE - REALPAD) == 0;
1773 } 1774 }
1775
1776 void floatToBuffer(OutBuffer buf, Type type, real value)
1777 {
1778 /* In order to get an exact representation, try converting it
1779 * to decimal then back again. If it matches, use it.
1780 * If it doesn't, fall back to hex, which is
1781 * always exact.
1782 */
1783 char[25] buffer;
1784 sprintf(buffer.ptr, "%Lg", value);
1785 assert(strlen(buffer.ptr) < buffer.length);
1786 //#ifdef _WIN32 && __DMC__
1787 // char *save = __locale_decpoint;
1788 // __locale_decpoint = ".";
1789 // real_t r = strtold(buffer, NULL);
1790 // __locale_decpoint = save;
1791 //#else
1792 real r = strtold(buffer.ptr, null);
1793 //#endif
1794 if (r == value) // if exact duplication
1795 buf.writestring(buffer);
1796 else
1797 buf.printf("%La", value); // ensure exact duplication
1798
1799 if (type)
1800 {
1801 Type t = type.toBasetype();
1802 switch (t.ty)
1803 {
1804 case Tfloat32:
1805 case Timaginary32:
1806 case Tcomplex32:
1807 buf.writeByte('F');
1808 break;
1809
1810 case Tfloat80:
1811 case Timaginary80:
1812 case Tcomplex80:
1813 buf.writeByte('L');
1814 break;
1815
1816 default:
1817 break;
1818 }
1819 if (t.isimaginary())
1820 buf.writeByte('i');
1821 }
1822 }