Re: How to make a Currency class from std.BigInt?

2015-01-31 Thread RuZzz via Digitalmars-d-learn

Thanks. Do I need to use rational fractions for the Currencies
class?


Re: How to make a Currency class from std.BigInt?

2015-01-31 Thread RuZzz via Digitalmars-d-learn

On Saturday, 31 January 2015 at 15:02:09 UTC, Colin wrote:
Maybe you can articulate it in one post what this class is 
trying to achieve?

Maybe this?
https://github.com/andersonpd/decimal/blob/master/decimal/bigfloat.d

The main thing is not to lose currency in calculations that use 
arithmetic.


Re: Labels in struct

2015-01-31 Thread tcak via Digitalmars-d-learn

On Saturday, 31 January 2015 at 16:04:36 UTC, tcak wrote:
I do not have a big example in the end to show, but is there 
any way to put labels into struct definitions?


struct CommunicationMessage{
char[2] signature;

mainData:
int info1;
int info2;

extraData:
ushort checksum;

content:
}


Example I defined something like above. I am using it as a base 
structure, and don't know how long the content of message will 
be. But I know that it will be at the end. I could use that 
content label to find out about end of struct. But 
unfortunately, it doesn't seem like it is supported.


I could say void* endOfStruct = struct + sizeof(struct), but 
then struct wouldn't be self explanatory with that content 
label at the end.


... **without** that content label ...


Re: Labels in struct

2015-01-31 Thread Adam D. Ruppe via Digitalmars-d-learn
You could use a UDA like that, though getting the offset will be 
a bit tricky.



The idea is to use some uda type, even plain string is good 
enough, then use the __traits to find the first appearance of 
that UDA in the object and return that offset.


Keep in mind that the @uda: syntax applies it to ALL following 
members, it isn't really a label, but if we only look at the 
first time it shows up we can kinda pretend it is one.


Here's an example:


struct Foo {
int b;
@(label_one): // offset 4
int c;
int d;
@(label_two): // offset 12
int e;
}

// get the offset of the label on a type, see below for usage
size_t offsetOf(T, string label)() {
foreach(memberName; __traits(allMembers, T)) {
		foreach(attribute; __traits(getAttributes, __traits(getMember, 
T, memberName))) {
			static if(is(typeof(attribute) == string)  attribute == 
label)

return __traits(getMember, T, 
memberName).offsetof;
}
}

assert(0, no such label);
}

void main() {
   // gives what we expect
pragma(msg, offsetOf!(Foo, label_one));
pragma(msg, offsetOf!(Foo, label_two));
}


Re: Labels in struct

2015-01-31 Thread Adam D. Ruppe via Digitalmars-d-learn
Oh this hack also won't work with a label at the very end. You 
could use sizeof for that though (however that would include 
padding bytes. Perhaps last member's offsetof plus last member's 
sizeof is a bit more accurate.)


Re: How to make a Currency class from std.BigInt?

2015-01-31 Thread RuZzz via Digitalmars-d-learn

The next version, which does not work:
https://bpaste.net/show/b9c85de68d07


Re: How to make a Currency class from std.BigInt?

2015-01-31 Thread zeljkog via Digitalmars-d-learn

On 31.01.15 15:45, RuZzz wrote:

Maybe I need good examples on C++, for this class, because a lot of
examples on C++.


Maybe this can help you:
https://github.com/andersonpd/decimal


Re: How to make a Currency class from std.BigInt?

2015-01-31 Thread RuZzz via Digitalmars-d-learn

I want to understand the correct architecture of the class.


Re: How to make a Currency class from std.BigInt?

2015-01-31 Thread Ivan Kazmenko via Digitalmars-d-learn

On Saturday, 31 January 2015 at 13:45:22 UTC, RuZzz wrote:

I want to understand the correct architecture of the class.


Sorry, you still did not state your problem (or what you are 
trying to achieve) clearly.
Writing down a clear problem description is likely to get you 
halfway to the solution.


Error when profiling

2015-01-31 Thread Phil via Digitalmars-d-learn
When trying to run my program with profiling enabled it dies 
before the first line of my main function runs. Everything works 
fine without profiling. I get the following stack trace:


 thread #1: tid = 0x38de4, 0x00010008d985 
vision_entry`gc_malloc + 49, queue = 'com.apple.main-thread', 
stop reason = EXC_BAD_ACCESS (code=1, address=0x0)

frame #0: 0x00010008d985 vision_entry`gc_malloc + 49
vision_entry`gc_malloc + 49:
- 0x10008d985:  movq   (%rdi), %rbx
   0x10008d988:  callq  *0x60(%rbx)
   0x10008d98c:  popq   %rbx
   0x10008d98d:  movq   %rbp, %rsp
(lldb) thread backtrace
* thread #1: tid = 0x38de4, 0x00010008d985 
vision_entry`gc_malloc + 49, queue = 'com.apple.main-thread', 
stop reason = EXC_BAD_ACCESS (code=1, address=0x0)

  * frame #0: 0x00010008d985 vision_entry`gc_malloc + 49
frame #1: 0x0001000908f5 vision_entry`_d_newclass + 117
frame #2: 0x0001000b4b28 
vision_entry`D3std9exception7bailOutFNaNfAyamxAaZv + 40
frame #3: 0x0001000b4d06 
vision_entry`D3std9exception14__T7enforceTbZ7enforceFNaNfbLAxaAyamZb 
+ 94
frame #4: 0x0001000c4df6 
vision_entry`D3std5stdio4File17LockingTextWriter6__ctorMFNcNeKS3std5stdio4FileZS3std5stdio4File17LockingTextWriter 
+ 86
frame #5: 0x0001000c4f19 
vision_entry`D3std5stdio4File17lockingTextWriterMFZS3std5stdio4File17LockingTextWriter 
+ 41
frame #6: 0x00012c74 
vision_entry`D3std5stdio4File15__T8writeflnTaZ8writeflnMFxAaZv + 
124 at stdio.d:1238
frame #7: 0x00010c06 vision_entry`_Dmain + 86 at 
stdio.d:2727
frame #8: 0x00010009060c 
vision_entry`D2rt6dmain211_d_run_mainUiPPaPUAAaZiZ6runAllMFZ9__lambda1MFZv 
+ 40
frame #9: 0x000100090551 
vision_entry`D2rt6dmain211_d_run_mainUiPPaPUAAaZiZ7tryExecMFMDFZvZv 
+ 45
frame #10: 0x0001000905b1 
vision_entry`D2rt6dmain211_d_run_mainUiPPaPUAAaZiZ6runAllMFZv + 45
frame #11: 0x000100090551 
vision_entry`D2rt6dmain211_d_run_mainUiPPaPUAAaZiZ7tryExecMFMDFZvZv 
+ 45

frame #12: 0x0001000904cd vision_entry`_d_run_main + 433
frame #13: 0x00010d29 vision_entry`main + 65
frame #14: 0x7fff883a45c9 libdyld.dylib`start + 1

Does anyone have any ideas of what could have caused this?


Re: How to make a Currency class from std.BigInt?

2015-01-31 Thread zeljkog via Digitalmars-d-learn

On 31.01.15 15:56, zeljkog wrote:

On 31.01.15 15:45, RuZzz wrote:

Maybe I need good examples on C++, for this class, because a lot of
examples on C++.


Maybe this can help you:
https://github.com/andersonpd/decimal


Also
http://code.dlang.org/packages/eris


Labels in struct

2015-01-31 Thread tcak via Digitalmars-d-learn
I do not have a big example in the end to show, but is there any 
way to put labels into struct definitions?


struct CommunicationMessage{
char[2] signature;

mainData:
int info1;
int info2;

extraData:
ushort checksum;

content:
}


Example I defined something like above. I am using it as a base 
structure, and don't know how long the content of message will 
be. But I know that it will be at the end. I could use that 
content label to find out about end of struct. But 
unfortunately, it doesn't seem like it is supported.


I could say void* endOfStruct = struct + sizeof(struct), but 
then struct wouldn't be self explanatory with that content label 
at the end.


Re: How to make a Currency class from std.BigInt?

2015-01-31 Thread RuZzz via Digitalmars-d-learn
Maybe I need good examples on C++, for this class, because a lot 
of examples on C++.


Re: How to make a Currency class from std.BigInt?

2015-01-31 Thread RuZzz via Digitalmars-d-learn

I am looking for not only free solutions.


Re: How to make a Currency class from std.BigInt?

2015-01-31 Thread Colin via Digitalmars-d-learn

On Saturday, 31 January 2015 at 14:26:45 UTC, RuZzz wrote:

The next version, which does not work:
https://bpaste.net/show/b9c85de68d07


I really dont understand what your trying to achieve here.

Maybe you can articulate it in one post what this class is trying 
to achieve?


Re: Labels in struct

2015-01-31 Thread Artur Skawina via Digitalmars-d-learn
On 01/31/15 17:04, tcak via Digitalmars-d-learn wrote:
 
 struct CommunicationMessage{
[...]
 content:
 }
 
 
 Example I defined something like above. I am using it as a base structure, 
 and don't know how long the content of message will be. But I know that it 
 will be at the end. I could use that content label to find out about end of 
 struct. But unfortunately, it doesn't seem like it is supported.
 
 I could say void* endOfStruct = struct + sizeof(struct), but then struct 
 wouldn't be self explanatory with that content label at the end.

The traditional way (ie C-like) would be

   ubyte[0] content; // zero-sized; use casts etc to access data.

as the last member. D supports that too, and just like
many other D features it works for ~80% of cases. IOW
you should be able to get it to work, but you might run
into problems if you need to access/manipulate such types.

artur


Re: Labels in struct

2015-01-31 Thread zeljkog via Digitalmars-d-learn

On 31.01.15 17:04, tcak wrote:

I do not have a big example in the end to show, but is there any way to
put labels into struct definitions?

struct CommunicationMessage{
 char[2] signature;

mainData:
 int info1;
 int info2;

extraData:
 ushort checksum;

content:
}



Members of type struct are actualy labels.

struct CommunicationMessage{
struct Md {
int info1;
int info2;
};
struct Ed {
ushort checksum;
}
struct Cnt {
}
char[2] signature;
Md mainData;
Ed extraData;
Cnt content;
}



Re: How to make a Currency class from std.BigInt?

2015-01-31 Thread RuZzz via Digitalmars-d-learn

The next version
https://bpaste.net/show/9468f24d9df0


Re: foreach - premature optimization vs cultivating good habits

2015-01-31 Thread via Digitalmars-d-learn

On Friday, 30 January 2015 at 14:41:11 UTC, Laeeth Isharc wrote:
Thanks, Adam.  That's what I had thought (your first 
paragraph), but something Ola on a different thread confused me 
and made me think I didn't understand it, and I wanted to pin 
it down.


There is always significant optimization effects in long running 
loops:

- SIMD
- cache locality / prefetching

For the former (SIMD) you need to make sure that good code is 
generated either by hand, by using vectorized libraries or by 
auto vectorization.


For the latter (cache) you need to make sure that the prefetcher 
is able to predict or is being told to prefetch explicitly and 
also that the working set is small enough to stay at the faster 
cache levels.


If you want good performance you cannot ignore any of these, and 
you have to design the data structures and algorithms for it. 
Prefetching has to happen maybe 100 instructions before the 
actual load from memory and AVX requires byte alignment and a 
layout that fits the algorithm. On next gen Xeon Skylake I think 
the alignment might go up to 64 byte and you have 512 bits wide 
registers (so you can do 8 64 bit floating point operations in 
parallel per core). The difference between issuing 1-4 ops and 
issuing 8-16 per time unit is noticable...


An of course, the closer your code is to theoretical throughput 
in the CPU, the more critical it becomes to not wait for memory 
loads.


This is also a moving target...


windows wininet library

2015-01-31 Thread ketmar via Digitalmars-d-learn
how can i use wininet.dll from D? i got bindings from https://github.com/
CS-svnmirror/dsource-bindings-win32.git, and then i tried to create 
wininet.lib with implib.exe from DMC package. no matter how i tried, the 
resulting wininet.lib seems to not work, as linker keep complaining 
about missing symbols like _InternetReadFile@16 and so on.

i'm building for win32.

ah, and yep, i compiled wininet.lib to lib dir, and added option to 
dmd.exe: -L+wininet.lib.

i assume that i have to create corrent .def file to remap all the names, 
am i right? oh, delightful...

signature.asc
Description: PGP signature


Re: How to make a Currency class from std.BigInt?

2015-01-31 Thread RuZzz via Digitalmars-d-learn
How to get amount of digit after point in a double for to get 
integer from a double?

assert(amountAfterPoint(1.456) == 3);
assert(amountAfterPoint(0.6) == 5);


Re: How to make a Currency class from std.BigInt?

2015-01-31 Thread RuZzz via Digitalmars-d-learn

without to!string


Re: How to make a Currency class from std.BigInt?

2015-01-31 Thread Colin via Digitalmars-d-learn

On Saturday, 31 January 2015 at 12:07:23 UTC, RuZzz wrote:
How to get amount of digit after point in a double for to get 
integer from a double?

assert(amountAfterPoint(1.456) == 3);
assert(amountAfterPoint(0.6) == 5);


How about a loop like

import std.stdio;

void main(){
writefln(%s, 1.45.numDigits);// prints 2
writefln(%s, 1.452343.numDigits);// prints 6
writefln(%s, 1.0.numDigits);// prints 0
}

long numDigits(double num){
 long i=0;
 double n=num;
 while(true){
if(n - (cast(long)n) == 0){
return i;
}
i++;
 n *= 10;
 }
}