Re: opOpAssign on object properties

2017-01-08 Thread collerblade via Digitalmars-d-learn

On Sunday, 8 January 2017 at 21:50:12 UTC, Ivan Kazmenko wrote:

On Sunday, 8 January 2017 at 18:23:34 UTC, collerblade wrote:

[...]


Hmm, right.

The setter is not called, and it's by the spec.
Which says that "a op= b" is rewritten as "a.opOpAssign !(op) 
(b)".

Here: https://dlang.org/spec/operatoroverloading.html#op-assign

[...]




Y, i have came up the same conclusion. I have to place an event 
like system into the Point.


Ty


Re: Need some help understanding PyD

2017-01-08 Thread Saurabh Das via Digitalmars-d-learn

PS: Noticed something off. My python installation is 3.4.3:
Python 3.4.3 (default, Sep 14 2016, 12:36:27)
[GCC 4.8.4] on linux

However when I run:
context.py_stmts("import sys");
context.py_stmts("print(sys.version)");

I get:
3.4.0 (default, Apr 11 2014, 13:08:40)
[GCC 4.8.2]


Also, when I import numpy from python3, it works, but if I do:
context.py_stmts("import numpy");

I get this error:
pyd.exception.PythonException@source/app.d(20):
ImportError: 
/usr/lib/python3.4/lib-dynload/_ctypes.cpython-34m-x86_64-linux-gnu.so: undefined symbol: _PyTraceback_Add





Need some help understanding PyD

2017-01-08 Thread Saurabh Das via Digitalmars-d-learn
I've been giving PyD a try. It's really nice and mostly 
everything works out of the box.


I'm trying to use TensorFlow in D via Pytho, so I need to call 
Python functions in D.


When I try to do:

auto context = new InterpContext();
context.py_stmts("import tensorflow");

I get this error:

pyd.exception.PythonException@source/app.d(19):
ImportError: 
/usr/lib/python3.4/lib-dynload/_ctypes.cpython-34m-x86_64-linux-gnu.so: undefined symbol: _PyTraceback_Add


../../.dub/packages/pyd-0.9.8/pyd/infrastructure/pyd/exception.d:46 void 
pyd.exception.handle_exception(immutable(char)[], ulong) [0x5917f6]
../../.dub/packages/pyd-0.9.8/pyd/infrastructure/pyd/embedded.d:147 void 
pyd.embedded.InterpContext.py_stmts(immutable(char)[], immutable(char)[], 
ulong) [0x59149a]
source/app.d:13 _Dmain [0x573cb2]
??:? 
_D2rt6dmain211_d_run_mainUiPPaPUAAaZiZ6runAllMFZ9__lambda1MFZv 
[0x5abfae]
??:? void rt.dmain2._d_run_main(int, char**, extern (C) int 
function(char[][])*).tryExec(scope void delegate()) [0x5abf04]
??:? void rt.dmain2._d_run_main(int, char**, extern (C) int 
function(char[][])*).runAll() [0x5abf6a]
??:? void rt.dmain2._d_run_main(int, char**, extern (C) int 
function(char[][])*).tryExec(scope void delegate()) [0x5abf04]

??:? _d_run_main [0x5abe61]
??:? main [0x575e6f]
??:? __libc_start_main [0x3fffbec4]
Program exited with code 1

How can I debug what the problem is?

Thanks,
Saurabh



Re: Getch() Problem: C vs D

2017-01-08 Thread Era Scarecrow via Digitalmars-d-learn

On Sunday, 8 January 2017 at 21:19:15 UTC, LouisHK wrote:
And works fine, but the D version below nothing happens when I 
hit ESCAPE:


Is this a bug or there is another approach?


Could this be because of maybe somehow it handles the console 
input? Kinda like how shift and different keys are toggles rather 
than dedicated to specific codes?


 Regardless, try ^[ ( Ctrl+[ ), which is 27 and ^] is 29.


Re: Mixin in Inline Assembly

2017-01-08 Thread Adam D. Ruppe via Digitalmars-d-learn

On Monday, 9 January 2017 at 02:31:42 UTC, Chris M. wrote:

asm
{
mov EAX, tmp; // I'd also like to know if I could just 
load *op1 directly into EAX

mov ECX, op2[EBP];
mixin(ins ~ " EAX, CL;"); // Issue here
mov tmp, EAX;
}
*op1 = tmp;
}

However, the inline assembler doesn't like me trying to do a 
mixin. Is there a way around this?

'

You should be able to break it up too

asm {
   mov EAX, tmp;
}
mixin("asm { "~ ins ~ "EAX, CL;" }");
asm {
   move tmp, EAX;
}


you get the idea. It should compile to the same thing.


Re: Mixin in Inline Assembly

2017-01-08 Thread Chris M. via Digitalmars-d-learn

On Monday, 9 January 2017 at 02:38:01 UTC, Stefan Koch wrote:

On Monday, 9 January 2017 at 02:31:42 UTC, Chris M. wrote:

[...]


Yes make the whole inline asm a mixin.


Awesome, got it working. Thanks to both replies.


Re: Mixin in Inline Assembly

2017-01-08 Thread ketmar via Digitalmars-d-learn

On Monday, 9 January 2017 at 02:31:42 UTC, Chris M. wrote:
However, the inline assembler doesn't like me trying to do a 
mixin.


yep. iasm is completely independent from other fronted, it has 
it's own lexer, parser and so on. don't expect those things to 
work. the only way is to mixin the whole iasm block, including 
`asm{}`.


Re: Mixin in Inline Assembly

2017-01-08 Thread Stefan Koch via Digitalmars-d-learn

On Monday, 9 January 2017 at 02:31:42 UTC, Chris M. wrote:
Right now I'm working on a project where I'm implementing a VM 
in D. I'm on the rotate instructions, and realized I could 
*almost* abstract the ror and rol instructions with the 
following function


private void rot(string ins)(int *op1, int op2)
{
int tmp = *op1;
asm
{
mov EAX, tmp; // I'd also like to know if I could just 
load *op1 directly into EAX

mov ECX, op2[EBP];
mixin(ins ~ " EAX, CL;"); // Issue here
mov tmp, EAX;
}
*op1 = tmp;
}

However, the inline assembler doesn't like me trying to do a 
mixin. Is there a way around this?


(There is a reason op1 is a pointer instead of a ref int, 
please don't ask about it)


Yes make the whole inline asm a mixin.


Mixin in Inline Assembly

2017-01-08 Thread Chris M. via Digitalmars-d-learn
Right now I'm working on a project where I'm implementing a VM in 
D. I'm on the rotate instructions, and realized I could *almost* 
abstract the ror and rol instructions with the following function


private void rot(string ins)(int *op1, int op2)
{
int tmp = *op1;
asm
{
mov EAX, tmp; // I'd also like to know if I could just 
load *op1 directly into EAX

mov ECX, op2[EBP];
mixin(ins ~ " EAX, CL;"); // Issue here
mov tmp, EAX;
}
*op1 = tmp;
}

However, the inline assembler doesn't like me trying to do a 
mixin. Is there a way around this?


(There is a reason op1 is a pointer instead of a ref int, please 
don't ask about it)


Re: Android Status

2017-01-08 Thread Ignacious via Digitalmars-d-learn

On Sunday, 8 January 2017 at 22:19:31 UTC, Joakim wrote:

On Sunday, 8 January 2017 at 21:52:01 UTC, Ignacious wrote:
Not sure what is going on, of course ;) So much BS just to do 
something that is suppose to be simple ;)



test.d



void main()
{

}

here is test.o

http://pastebin.com/NRrKgKtb

Any ideas?


Oh, that's easy: install the NDK too, as shown on the wiki.  
You need the linker that supports ARM from the NDK.  Follow the 
instructions from the wiki to compile and link the binary, 
simply having ldc do everything won't work.




Ok, after executing

$NDK/toolchains/llvm/prebuilt/linux-x86_64/bin/clang 
-Wl,-z,nocopyreloc --sysroot=$NDK/platforms/android-9/arch-arm 
-lgcc -gcc-toolchain 
$NDK/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64 
-target armv7-none-linux-androideabi -no-canonical-prefixes 
-fuse-ld=bfd -Wl,--fix-cortex-a8 -Wl,--no-undefined 
-Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now -fPIE -pie -mthumb 
-Wl,--export-dynamic -lc -lm test.o lib/libphobos2-ldc.a 
lib/libdruntime-ldc.a -o test


I get a test elf file with no errors(although 2.5MB for a hello 
world).


I had to do the chmod 755 test

then

./test

to get any output. Before that no output and no errors so wasn't 
sure what as going on.


Looks like everything is working! ;)

Seems like someone really needs to put some time in to getting 
all this stuff organized and situated


Maybe the D language foundation can push some money towards it to 
get it started off on the right foot?


I'll try to get some of the opengl examples on your repository to 
see if they work soon.



Cross-compiler toolchains are never simple, consider yourself 
lucky for having gotten off easy. :)


I realize things are difficult but it's people that make it that 
way ;) Life would be so much simpler if people would just 
properly document stuff exactly(or, rather, do what they are 
suppose to do). (Even windows seems to love to forget to put in 
descriptions of services, tasks, application descriptions, etc).


The main problem I have had seems to be that UoW uses ver 14. 
Somehow I was able to upgrade by following docs online(wasn't 
easy but eventually got there and everything seems to work... I 
should have documented ;) but I wasn't sure if the process would 
work. Supposedly ver 16 exists by one has to be part of the dev 
team or something.




Re: Android Status

2017-01-08 Thread Joakim via Digitalmars-d-learn

On Sunday, 8 January 2017 at 21:52:01 UTC, Ignacious wrote:
Not sure what is going on, of course ;) So much BS just to do 
something that is suppose to be simple ;)



test.d



void main()
{

}

here is test.o

http://pastebin.com/NRrKgKtb

Any ideas?


Oh, that's easy: install the NDK too, as shown on the wiki.  You 
need the linker that supports ARM from the NDK.  Follow the 
instructions from the wiki to compile and link the binary, simply 
having ldc do everything won't work.


Cross-compiler toolchains are never simple, consider yourself 
lucky for having gotten off easy. :)


Re: Primality test function doesn't work on large numbers?

2017-01-08 Thread Timon Gehr via Digitalmars-d-learn

On 08.01.2017 08:52, Elronnd wrote:

I'm working on writing an RSA implementation, but I've run into a
roadblock generating primes.  With a more than 9 bits, my program either
hangs for a long time (utilizing %100 CPU!) or returns a composite
number.  With 9 or fewer bits, I get primes, but I have to run with a
huge number of iterations to actually _get_ a random number.  It runs
fast, though.  Why might this be?  Code:
http://lpaste.net/1034777940820230144


Fixed version:

import std.bigint;
import std.stdio;

private alias bigint = BigInt;
bigint pow(bigint base,bigint exponent){
bigint tmp=1;
for(auto x=base,y=exponent;y;x*=x,y/=2)
if(y%2) tmp*=x;
return tmp;
}
bigint powm(bigint base,bigint exponent,bigint modulus){
bigint tmp=1;
for(auto x=base,y=exponent;y;x=x*x%modulus,y/=2)
if(y%2) tmp=tmp*x%modulus;
return tmp;
}
private pragma(inline, true) bigint pow(int base, bigint exponent) {
return pow(bigint(base), exponent);
}
private pragma(inline, true) bigint pow(bigint base, int exponent) {
return pow(base, bigint(exponent));
}
private pragma(inline, true) bigint pow(int base, int exponent) {
return pow(bigint(base), bigint(exponent));
}

// Credit to 
http://csrc.nist.gov/publications/fips/fips186-3/fips_186-3.pdf appendix C.3

private bigint genprime(int bits, int numtests){
import std.random: uniform;

bool mrprime(bigint integer, int iterations) {
if(integer%2==0)
return false;

bigint m, a = 0, tmp = integer, b, z;
int length;

for(m=integer-1;m%2==0;m/=2,++a){}
assert((integer-1)%pow(bigint(2), a)==0);

while(tmp != 0) {
tmp >>=1;
length += 1;
}

for (int i=0; i// Create b such that b has the same number of bits as 
"integer"

for (int j = 1; j<=length; j++) {
b <<= 1;
b += uniform(0, 2);
}
while ((b <= 1) || (b >= (integer-1))) {
b = 0;
for (int j = 1; j<=length; j++) {
b <<= 1;
b += uniform(0, 2);
}
}

z = powm(b, m, integer);
if((z == 1) || (z == integer-1))
goto endofloop;

for(int k=1; k<=a-1; k++) {
z = z*z%integer;
if(z == integer-1)
goto endofloop;
if(z == 1)
return false;
}
return false;
endofloop:
}
return true;
}

bigint genbigint(int numbits) {
bigint tmp;
while (numbits --> 0) {
tmp <<= 1;
tmp += uniform(0, 2);
}
return tmp;
}
bigint currnum;
while (true) {
currnum = genbigint(bits);
if (mrprime(currnum, numtests)) {
return currnum;
}
}
assert(0);
}

void main(){
writeln(genprime(300,30));
}



Re: Android Status

2017-01-08 Thread Ignacious via Digitalmars-d-learn

On Sunday, 8 January 2017 at 20:34:21 UTC, Joakim wrote:

On Sunday, 8 January 2017 at 19:58:06 UTC, Ignacious wrote:
I suppose it will be easier to install a real ubuntu distro 
rather than relying on windows? All these issues seem to be 
related to outdated versions?


Distributor ID: Ubuntu
Description:Ubuntu 14.04.5 LTS
Release:14.04
Codename:   trusty


I doubt that'd work either as Debian just uses older packages.  
Your best bet may be to just compile ldc yourself, by following 
the instructions on the wiki.


Well, I finally got it to upgrade to 16.. when I run ldc2 or 
ldmd2 I get the following errors


/usr/bin/ld: test.o: Relocations in generic ELF (EM: 40)
/usr/bin/ld: test.o: Relocations in generic ELF (EM: 40)
/usr/bin/ld: test.o: Relocations in generic ELF (EM: 40)
/usr/bin/ld: test.o: Relocations in generic ELF (EM: 40)
/usr/bin/ld: test.o: Relocations in generic ELF (EM: 40)
/usr/bin/ld: test.o: Relocations in generic ELF (EM: 40)
/usr/bin/ld: test.o: Relocations in generic ELF (EM: 40)
test.o: error adding symbols: File in wrong format
collect2: error: ld returned 1 exit status
Error: /usr/bin/gcc failed with status: 1

test is just a simple hello world.



root@:/mnt/b/DLang/ldc2Android# gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/5/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 
5.4.0-6ubuntu1~16.04.4' 
--with-bugurl=file:///usr/share/doc/gcc-5/README.Bugs 
--enable-languages=c,ada,c++,java,go,d,fortran,objc,obj-c++ 
--prefix=/usr --program-suffix=-5 --enable-shared 
--enable-linker-build-id --libexecdir=/usr/lib 
--without-included-gettext --enable-threads=posix 
--libdir=/usr/lib --enable-nls --with-sysroot=/ 
--enable-clocale=gnu --enable-libstdcxx-debug 
--enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new 
--enable-gnu-unique-object --disable-vtable-verify 
--enable-libmpx --enable-plugin --with-system-zlib 
--disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo 
--with-java-home=/usr/lib/jvm/java-1.5.0-gcj-5-amd64/jre 
--enable-java-home 
--with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-5-amd64 
--with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-5-amd64 
--with-arch-directory=amd64 
--with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc 
--enable-multiarch --disable-werror --with-arch-32=i686 
--with-abi=m64 --with-multilib-list=m32,m64,mx32 
--enable-multilib --with-tune=generic --enable-checking=release 
--build=x86_64-linux-gnu --host=x86_64-linux-gnu 
--target=x86_64-linux-gnu

Thread model: posix
gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.4)


Not sure what is going on, of course ;) So much BS just to do 
something that is suppose to be simple ;)



test.d



void main()
{

}

here is test.o

http://pastebin.com/NRrKgKtb

Any ideas?




Re: opOpAssign on object properties

2017-01-08 Thread Ivan Kazmenko via Digitalmars-d-learn

On Sunday, 8 January 2017 at 18:23:34 UTC, collerblade wrote:

On Sunday, 8 January 2017 at 10:03:50 UTC, Ivan Kazmenko wrote:

On Sunday, 8 January 2017 at 09:22:12 UTC, collerblade wrote:

[...]


1. If you want the member variable to change, naturally, you 
should provide a getter property which returns a reference to 
that variable:


[...]


yes i tried the reference return, but the problem is, that the 
setter does NOT gets called, no matter what the result type of 
the opOpAssign method is.

I want to detect changes, but this way i still not able.

A a = new A;

a.location+=Point(1,1); //the private value changes, but the 
setter does not get called


Hmm, right.

The setter is not called, and it's by the spec.
Which says that "a op= b" is rewritten as "a.opOpAssign !(op) 
(b)".

Here: https://dlang.org/spec/operatoroverloading.html#op-assign

So, no *assignment* happens when you call a.location+=Point(1,1).
To have a side effect triggered by opAssign-ment, you can do it 
inside the opOpAssign function.


Looking at it another way, actions with struct Point can be seen 
as the responsibility of struct Point.  If you want to monitor 
these actions, do it in the code of opOpAssign function.  After 
that, your class A can inspect the state of its corresponding 
Point.  If normal Points don't need that, you can have a special 
SelfAwarePoint which has an alias this to its member Point.


Alternatively, you can have two getter properties: one as const 
and one by reference.  When the reference one gets called, you 
know the value of Point *may* have changed.


Well, I'm out of ideas for now.  If these still don't quite 
satisfy you, including a bigger picture of what you want to 
achieve may help.


Ivan Kazmenko.



Getch() Problem: C vs D

2017-01-08 Thread LouisHK via Digitalmars-d-learn

Hello, I minimize the problem to identify the problem:

Here the C version:

#include 
int main(){
   int c;
   while(c != 27){
   printf("%d\n", (c = getch()));
   }
   return 0;
}

And works fine, but the D version below nothing happens when I 
hit ESCAPE:


import std.stdio;
extern (C) int getch();
int main(){
   int c;
   while(c != 27){
   printf("%d\n", (c = getch()));
   }
   return 0;
}

Is this a bug or there is another approach?

Thanks,

L.





Re: Android Status

2017-01-08 Thread Joakim via Digitalmars-d-learn

On Sunday, 8 January 2017 at 19:58:06 UTC, Ignacious wrote:
I suppose it will be easier to install a real ubuntu distro 
rather than relying on windows? All these issues seem to be 
related to outdated versions?


Distributor ID: Ubuntu
Description:Ubuntu 14.04.5 LTS
Release:14.04
Codename:   trusty


I doubt that'd work either as Debian just uses older packages.  
Your best bet may be to just compile ldc yourself, by following 
the instructions on the wiki.


Re: Android Status

2017-01-08 Thread Ignacious via Digitalmars-d-learn


Yeah, not a good idea to build from source yourself.  Try the 
advice here, ie see if you can install a package with that 
library or just symlink to the older library if not:


http://askubuntu.com/questions/771047/erlang-error-while-loading-shared-libraries-libncursesw-so-6



Well, the only way to get it to work is rename 5.9 ver to 6.0, 
but then now I get the error


root@DESKTOP:/mnt/b/DLang/ldc2Android# bin/ldmd2 test.d
bin/ldmd2: error while loading shared libraries: 
libncursesw.so.6: cannot open shared object file: No such file or 
directory

root@DESKTOP:/mnt/b/DLang/ldc2Android# bin/ldmd2 test.d
bin/ldmd2: error while loading shared libraries: 
libncursesw.so.6: cannot open shared object file: No such file or 
directory

root@DESKTOP:/mnt/b/DLang/ldc2Android# bin/ldmd2 test.d
bin/ldmd2: /usr/lib/x86_64-linux-gnu/libstdc++.so.6: version 
`GLIBCXX_3.4.21' not found (required by bin/ldmd2)

root@DESKTOP:/mnt/b/DLang/ldc2Android# bin/ldc2 test.d
bin/ldc2: /usr/lib/x86_64-linux-gnu/libstdc++.so.6: version 
`GLIBCXX_3.4.20' not found (required by bin/ldc2)
bin/ldc2: /usr/lib/x86_64-linux-gnu/libstdc++.so.6: version 
`GLIBCXX_3.4.21' not found (required by bin/ldc2)


I followed this page:

http://askubuntu.com/questions/575505/glibcxx-3-4-20-not-found-how-to-fix-this-error

and now I get the error


bin/ldc2: relocation error: bin/ldc2: symbol 
_ZNKSt3_V214error_category10_M_messageB5cxx11Ei, version 
GLIBCXX_3.4.21 not defined in file libstdc++.so.6 with link time 
reference


even though

strings /usr/lib/x86_64-linux-gnu/libstdc++.so.6 | grep GLIBCXX
GLIBCXX_3.4
GLIBCXX_3.4.1
GLIBCXX_3.4.2
GLIBCXX_3.4.3
GLIBCXX_3.4.4
GLIBCXX_3.4.5
GLIBCXX_3.4.6
GLIBCXX_3.4.7
GLIBCXX_3.4.8
GLIBCXX_3.4.9
GLIBCXX_3.4.10
GLIBCXX_3.4.11
GLIBCXX_3.4.12
GLIBCXX_3.4.13
GLIBCXX_3.4.14
GLIBCXX_3.4.15
GLIBCXX_3.4.16
GLIBCXX_3.4.17
GLIBCXX_3.4.18
GLIBCXX_3.4.19
GLIBCXX_3.4.20
GLIBCXX_3.4.21
GLIBCXX_3.4.22
GLIBCXX_DEBUG_MESSAGE_LENGTH

I suppose it will be easier to install a real ubuntu distro 
rather than relying on windows? All these issues seem to be 
related to outdated versions?


Distributor ID: Ubuntu
Description:Ubuntu 14.04.5 LTS
Release:14.04
Codename:   trusty



Re: opOpAssign on object properties

2017-01-08 Thread collerblade via Digitalmars-d-learn

On Sunday, 8 January 2017 at 10:03:50 UTC, Ivan Kazmenko wrote:

On Sunday, 8 January 2017 at 09:22:12 UTC, collerblade wrote:

[...]


1. If you want the member variable to change, naturally, you 
should provide a getter property which returns a reference to 
that variable:


[...]


yes i tried the reference return, but the problem is, that the 
setter does NOT gets called, no matter what the result type of 
the opOpAssign method is.

I want to detect changes, but this way i still not able.

A a = new A;

a.location+=Point(1,1); //the private value changes, but the 
setter does not get called









Re: CTFE difference between dmd and ldc2

2017-01-08 Thread Joseph Rushton Wakeling via Digitalmars-d-learn
On Saturday, 7 January 2017 at 22:55:55 UTC, Joseph Rushton 
Wakeling wrote:
I should probably also create a formal issue for this.  Any 
thoughts on how best to break it down into a minimal example?  
It does not appear easy to do so at first glance :-\


Turned out to be easier than I anticipated.  It was not a CTFE 
problem but one of default initialization of struct fields:

https://issues.dlang.org/show_bug.cgi?id=17073

In short, `void` default initialization seems to take priority 
with dmd regardless of anything else.


Re: opOpAssign on object properties

2017-01-08 Thread Ivan Kazmenko via Digitalmars-d-learn

On Sunday, 8 January 2017 at 09:22:12 UTC, collerblade wrote:

How can i do opOpAssign with properties??


1. If you want the member variable to change, naturally, you 
should provide a getter property which returns a reference to 
that variable:


ref Point location() @property {
  return m_location;
}

This alone solves the immediate problem.

2. Note that it is common for assignment expressions to return a 
reference to the result, which would, for example, make chains 
like "a = (b += c)" possible:


  ref Point opOpAssign(string op)(in Point p) if (op == "+") {
x+=p.x;
y+=p.y;
return this;
  }

Here's a complete working version of your example:

-
struct Point {
  float x=0,y=0;

  this(float _x, float _y) {
x=_x;
y=_y;
  }

  //opopassign for +=
  ref Point opOpAssign(string op)(in Point p) if (op == "+") {
x+=p.x;
y+=p.y;
return this;
  }
}

class A {
  public:
ref Point location() @property {
  return m_location;
}

void location(in Point newlocation) @property {
  m_location=newlocation;
}

  private:
Point m_location;
}

void main() {
  import std.stdio;
  auto a= new A;
  a.location+=Point(1,1);
  writeln (a.location); // Point(1, 1)
  a.location+=Point(1,1);
  writeln (a.location); // Point(2, 2)
}
-

Ivan Kazmenko.


Re: Primality test function doesn't work on large numbers?

2017-01-08 Thread Eugene Wissner via Digitalmars-d-learn

On Sunday, 8 January 2017 at 07:52:33 UTC, Elronnd wrote:
I'm working on writing an RSA implementation, but I've run into 
a roadblock generating primes.  With a more than 9 bits, my 
program either hangs for a long time (utilizing %100 CPU!) or 
returns a composite number.  With 9 or fewer bits, I get 
primes, but I have to run with a huge number of iterations to 
actually _get_ a random number.  It runs fast, though.  Why 
might this be?  Code: http://lpaste.net/1034777940820230144


I haven't read your code very exactly, but I have an assumption 
and you can check if it is helpful:)


I think your actual problem is this line:

z = pow(b, m) % integer;

If it does what I think, it can be horribly slow and memory 
consuming. You have to implement your own pow function that does 
a ^ b mod c. Look into python source code or in "tanya" (D): 
https://github.com/caraus-ecms/tanya/blob/master/source/tanya/math/package.d. It is the same algorithm that phobos uses but with modulo operation built-in and a bit differently written (my code is based neither on python nor on phobos and uses a different bigint implementation). You can also rewrite pow(z, 2) % integer then. It will be faster.

Try to reduce bigint copying and arithmetic anyway if possible.


opOpAssign on object properties

2017-01-08 Thread collerblade via Digitalmars-d-learn

hello guys,
i would like to have properties with /= *= += -= operators. My 
code:


struct Point {
  float x=0,y=0;

  this(float _x, float _y) {
x=_x;
y=_y;
  }

  //opassign for +

  //opopassign for +=
  void opOpAssign(string op=="+")(in Point p) {
x+=p.x;
y+=p.y;
  }
}

class A {
  public:
Point location() const @property {
  return m_location;
}

void location(in Point newlocation) @property {
  m_location=newlocation;
}

  private:
Point m_location;
}

void main() {
  auto a= new A;
  a.location=a.location+Point(1,1); //DOES WORK
  a.location+=Point(1,1); //DOESN'T WORK
}



The problem is that this code is not working. The getter and the 
opOpAssign gets called, but the setter does not.

How can i do opOpAssign with properties??