Re: Weird floating point rounding - Bug or how to control it correctly

2023-09-13 Thread Basile B. via Digitalmars-d-learn

On Thursday, 14 September 2023 at 03:23:48 UTC, An Pham wrote:

import std.stdio;

void main()
{
float f = 6394763.345f;

import std.format : sformat;

char[80] vBuffer = void;
writeln("6394763.345 = ", sformat(vBuffer[], "%.4f", 
f));


}

Output
6394763.345 = 6394763.5000


Classic question. The float literal `6394763.345f` is not 
representable as IEEE-754 floating point number.


Try https://www.h-schmidt.net/FloatConverter/IEEE754.html for a 
short introduction to the issue.


Re: Weird floating point rounding - Bug or how to control it correctly

2023-09-13 Thread Jonathan M Davis via Digitalmars-d-learn
On Wednesday, September 13, 2023 9:23:48 PM MDT An Pham via Digitalmars-d-
learn wrote:
> import std.stdio;
>
>  void main()
>  {
>  float f = 6394763.345f;
>
>  import std.format : sformat;
>
>  char[80] vBuffer = void;
>  writeln("6394763.345 = ", sformat(vBuffer[], "%.4f", f));
>
>  }
>
> Output
> 6394763.345 = 6394763.5000

The nature of floating point numbers is such that there a bunch of values
that they can't actually represent, and they get rounded pretty easily
depending on the exact numbers involved. So, in general, you shouldn't
expect floating point numbers to be exact. You will generally do better with
increased precision though, and in this case, it looks like your number will
stay the same if you use double or real instead of float.

I would suggest that you watch this video from dconf 2016 which discusses
floating point values:

https://www.youtube.com/watch?v=YEUAUnamQiA

- Jonathan M Davis





Weird floating point rounding - Bug or how to control it correctly

2023-09-13 Thread An Pham via Digitalmars-d-learn

import std.stdio;

void main()
{
float f = 6394763.345f;

import std.format : sformat;

char[80] vBuffer = void;
writeln("6394763.345 = ", sformat(vBuffer[], "%.4f", f));

}

Output
6394763.345 = 6394763.5000


Dub option for specifying importC include paths

2023-09-13 Thread Ben Jones via Digitalmars-d-learn
I'm trying to build a wrapper around a lib written in C by having 
importC compile the .c files, and then providing a nice D API on 
top.  My dub config file seems to be finding the C files to 
compile without issue, but it's not passing the include dirs for 
the preprocessor to find headers.  Is there an option I'm missing 
or is there some way to get DUB to do this for me aside from 
passing like -cpp flags via dflags?


From my dub.json:

```
"cSourcePaths": [
"source/libgit2/src/",
"source/libgit2/src/util/"
],
"cImportPaths" : [
"source/libgit2/src/util/",
"source/libgit2/src/libgit2/"
],
"importPaths" : [
"source/libgit2/src/util/",
],

"excludedSourceFiles" : [
"source/libgit2/src/cli/*.c"
],
```

And the what dub's actually running when it preprocesses the 
first .c file:


```
/usr/bin/cc -fno-blocks -dD -Wno-builtin-macro-redefined -E 
-include 
/opt/homebrew/Cellar/ldc/1.34.0/include/dlang/ldc/importc.h 
source/libgit2/src/libgit2/annotated_commit.c -o 
/var/folders/p3/6fcprxgn0xb77hzb031vm1_mgn/T/itmp-ldc-de8fa3/annotated_commit.i

```

Which has doesn't specify any -I flags


Re: Struct nested function

2023-09-13 Thread Ferhat Kurtulmuş via Digitalmars-d-learn

On Wednesday, 13 September 2023 at 05:58:13 UTC, vino wrote:

Hi All,

  Request your help, I have a struct which has many functions, 
I need to run a function from within another function(From 
Display function execute the runner function), an example as 
below


From,
Vino


The problem starts here:

```string *runnerptr = ```

You are trying to assign a delegate to string*. Even if we fix 
this, we hit another error because D programming language does 
not allow capturing a reference to this, which is not permitted 
in tasks (IMHO). We fix this using a lambda ```auto result = 
task(() => runner());```. Then the final code that runs should be 
like:


```d
import std.stdio: writeln;
import std.algorithm: joiner;
import std.parallelism: task;
import std.typecons: tuple;

struct MainEngine {
int rno;
string firstname;
string lastname;
int age;

this(in int _rno) { rno = _rno; }
auto ref FirstName(in string _firstname) { firstname = 
_firstname; return this; }
auto ref LastName(in string _lastname) { lastname = 
_lastname; return this; }

auto ref Age(in int _age) { age = _age; return this; }
auto Display () {
auto runner(string status = "Male")
{
auto record = tuple([firstname,",",lastname].joiner, 
age, status);

return record;
}

auto result = task(() => runner());
//writeln(typeof(result).stringof);
result.executeInNewThread;
result.yieldForce;
return result;
}
}

void main () {
auto mrunner = 
MainEngine(1).FirstName("Fname").LastName("Lname").Age(25).Display();
writeln((*mrunner).yieldForce); // Access the result field to 
get the value returned by the task

}
```


Struct nested function

2023-09-13 Thread vino via Digitalmars-d-learn

Hi All,

  Request your help, I have a struct which has many functions, I 
need to run a function from within another function(From Display 
function execute the runner function), an example as below


```
import std.stdio: writeln;
import std.algorithm: joiner;
import std.parallelism: task;
import std.typecons: tuple;

struct MainEngine {
int rno;
string firstname;
string lastname;
int age;

this(in int _rno) { rno = _rno; }
auto ref FirstName(in string _firstname) { firstname = 
_firstname; return this; }
auto ref lastName(in string _lastname) { firstname = 
_lastname; return this; }

auto ref Age(in int _age) { age = _age; return this; }
auto Display () {

auto runner(string status = "Male") {
	auto record = tuple([firstname,",",lastname].joiner, 
age, status);

return record;
}
string *runnerptr = 

auto result = task(*runnerptr);
result.executeInNewThread;
result.yieldForce;
return result;
}
}

void main () {
auto mrunner = 
MainEngine(1).FirstName("Fname").lastName("Lname").Age(25).Display();

writeln(mrunner);
}
```

From,
Vino