October 08, 2012
On Sun, Oct 07, 2012 at 01:15:50PM +0400, Dmitry Olshansky wrote: [...]
> Just define method with this signature:
> void toString(scope void delegate(const(char)[]) sink)
> 
> And bingo! It's used in all of formatting stuff like write(f)(ln),
> format, formattedWrite etc.
> 
> e.g. before posting I played with this:
> 
> import std.stdio, std.format;
> 
> struct A{
> 	int k;
> 	void toString(scope void delegate(const(char)[]) sink)
> 	{
> 		formattedWrite(sink, "[%d]", k);
> 	}
> }
> 
> void main(){
> 	A a = A(90);
> 	writeln(a);
> }
[...]

+1. This is much more powerful and useful, not to mention more efficient
in many cases (by avoiding the need to create multiple string buffers),
than the current toString(). I like this.


T

-- 
We are in class, we are supposed to be learning, we have a teacher... Is it too much that I expect him to teach me??? -- RL
October 08, 2012
On Mon, 08 Oct 2012 09:28:37 +0200, H. S. Teoh <hsteoh@quickfur.ath.cx> wrote:

> I'd like to chime in to state that I'd love to have Rational support
> BigInt. If necessary, use an arbitrary-width floating point format for
> floating point conversions (BigFloat?).
>

Incidentally, I would very much need a BigFloat class/struct, written in D and independent of any C library.
I'm trying to write one myself, but it seems to be rather tricky. Could this be implemented in a short amount of time by someone with more knowledge about this topic?

-- 
My D Compiler: http://code.google.com/p/dil
October 08, 2012
On Mon, Oct 8, 2012 at 2:08 AM, Simen Kjaeraas <simen.kjaras@gmail.com> wrote:
> On 2012-44-08 06:10, Arlen <arlen.ng@gmx.com> wrote:
>
>>
>> 1.  To convert a BigInt to floating-point one needs to convert it to the built-in integer types first.  If you go beyond the limits of the built-in types (long), then what's the point?  You might as well be using int or long.
>
>
> I thought (part of) the point of Rational was to use it when it would be more fitting than floating-point. If it's only there to be converted to floating-point, I don't know what it's there for.
>

Some computations with rational numbers produce irrational numbers, and to store irrational numbers you need real numbers.  What do we do in those cases?

> As for a workaround, right-shift num and den to reasonable values
> (which fit in a long), divide to get a float, and multiply by
> 2.0^^(log2(num)-log2(den)). Oughta work.
>
>
>
>> 2.  The functions in std.math don't support BigInt, and most likely never will.  Even if they did, you would still need multi-precision floating point to store the irrational numbers.  If you decided to use the built-in types, then again what's the point?  You might as well go with int or long.
>
>
> The only function you use from std.math is abs, right? That should be fairly easy to implement for BigInt.
>
> It'd mean you'd have to specialize a bit for BigInt, though (just create your own abs function that calls the correct one).
>

Yes, std.math.abs and std.numeric.gcd are very easy to implement for
BigInt, but I'm not talking about those.  I'm talking about things
like std.math.pow(), and std.math.log().
October 08, 2012
On 10/08/2012 07:56 AM, Aziz K. wrote:
>
> Incidentally, I would very much need a BigFloat class/struct, written in
> D and independent of any C library.
> I'm trying to write one myself, but it seems to be rather tricky. Could
> this be implemented in a short amount of time by someone with more
> knowledge about this topic?
>

Wasn't Paul D. Anderson working on a BigFloat?
October 08, 2012
Arlen:

> Yes, std.math.abs and std.numeric.gcd are very easy to implement for BigInt,

Don doesn't agree regarding the gcd. To compute the gcd efficiently on large numbers you need not easy algorithms.

See:
http://d.puremagic.com/issues/show_bug.cgi?id=7102

Bye,
bearophile
October 09, 2012
On Mon, Oct 8, 2012 at 4:37 PM, bearophile <bearophileHUGS@lycos.com> wrote:
> Arlen:
>
>
>> Yes, std.math.abs and std.numeric.gcd are very easy to implement for BigInt,
>
>
> Don doesn't agree regarding the gcd. To compute the gcd efficiently on large numbers you need not easy algorithms.
>
> See:
> http://d.puremagic.com/issues/show_bug.cgi?id=7102
>
> Bye,
> bearophile


gcd() is very easy to implement:

BigInt _gcd(BigInt a, BigInt b)
{
  enforce(a >= 0 && b >=0);
  while (b != 0) {
    auto t = b;
    b = a % b;
    a = t;
  }
  return a;
}

It's not going to be the fastest or the most efficient, but it does produce correct results, and that's the most important thing when implementing mathematical functions IMO.  You can always improve the performance later.  Implementing pow(), on the other hand, is very difficult and complex.
October 09, 2012
On Mon, 08 Oct 2012 23:01:42 +0200, Ellery Newcomer <ellery-newcomer@utulsa.edu> wrote:

> Wasn't Paul D. Anderson working on a BigFloat?

Thanks for the hint. I found his project on github:

https://github.com/andersonpd/decimal

Seems fairly complete and usable. Hope he's going to continue to develop and maintain it.
October 09, 2012
On Monday, 8 October 2012 at 15:14:35 UTC, Aziz K. wrote:
>
> Incidentally, I would very much need a BigFloat class/struct, written in D and independent of any C library.
> I'm trying to write one myself, but it seems to be rather tricky. Could this be implemented in a short amount of time by someone with more knowledge about this topic?

Not in a short amount of time!! I've got a 95% complete library that handles BigDecimal numbers as well as Decimal32, Decimal64 and Decimal128 numbers. The BigDecimal numbers are fully implemented except for math functions (exp, ln, etc.)

http://www.dsource.org/projects/decimal

Paul

October 09, 2012
On Tue, 09 Oct 2012 19:32:02 +0200, Paul D. Anderson <paul.d.removethis.anderson@comcast.andthis.net> wrote:

> Not in a short amount of time!! I've got a 95% complete library that handles BigDecimal numbers as well as Decimal32, Decimal64 and Decimal128 numbers. The BigDecimal numbers are fully implemented except for math functions (exp, ln, etc.)
>
> http://www.dsource.org/projects/decimal
>
> Paul
>

So glad you're working on this. Keep up the good work! ;-)
1 2 3
Next ›   Last »