www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Conversion problem.

reply Den_d_y <tod.naz ya.ru> writes:
Hello! Here I am again, with my problem ... In my program, I 
cannot manage to convert from "double" to "int". Here is the code:
<< ++
struct Animation - the structure that implements the animation
+ /
struct Animation
{
private List! (Image) img; /// List of pictures in the animation
private double f; /// current picture in animation
private double s; /// speed of animation

/// speed setter (double s)
public void setSpeed ​​(double sp)
{
s = sp;
}

/// Adds a picture to the list
public void addFrame (Image pia)
{
img.add (pia);
}

/// Sets the image to a specific place in the list
public void setFrame (int id, Image pia)
{
img.set (id, pia);
}

/// Returns a picture from the list
public Image getFrame (int id)
{
return img.get (id);
}

/// Returns the list
public List! (Image) getList ()
{
return img;
}

/// The animation event itself. Returns the current animation 
image.
public Image step ()
{
writeln ("Animation :: step start ...");
if (floor (f) == img.length)
{
f = 0.1f;
writeln ("Animation :: f = 0.1f");
} else
{
f + = s;
writeln ("Animation :: f + = s");
}

const int i = roundTo! (int) (f);
writeln ("i set");
writeln ("i:", i);
return img.get (i);
}
} >>
Another code:
<< import std.stdio;

/ ++
The structure that implements the list of objects.
+ /
struct List (TObj)
{
private TObj [] obj;

/// Adds an object to the list
public void add (TObj o)
{
obj ~ = o;
}

/// Returns an object from the list
public TObj get (int id)
{
return obj [id];
}

/// Sets an object in a specific place.
public void set (int id, TObj o)
{
obj [id] = o;
}

/// Returns the length of the list
public int length ()
{
return obj.length;
}

/// Gives the garbage collector a signal to clear the list.
public void free ()
{
obj = null;
}
} >>

"If you don’t understand something, the translator has bricked 
it."

In the moment:
<< public Image step ()
{
writeln ("Animation :: step start ...");
if (floor (f) == img.length)
{
f = 0.1f;
writeln ("Animation :: f = 0.1f");
} else
{
f + = s;
writeln ("Animation :: f + = s");
}

const int i = roundTo! (int) (f); // Error here
writeln ("i set");
writeln ("i:", i);
return img.get (i);
} >> The compiler is silent, but when you start the program, 
reaching this place, it hangs.
  Is there any way to convert a "double" to an "int" without 
errors?
Jun 25 2019
parent reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Tue, Jun 25, 2019 at 12:08:07PM +0000, Den_d_y via Digitalmars-d-learn wrote:
 Hello! Here I am again, with my problem ... In my program, I cannot manage
 to convert from "double" to "int". Here is the code:
[...] Did you try this? import std.conv : to; double d = ...; int i = d.to!int; T -- People tell me I'm stubborn, but I refuse to accept it!
Jun 25 2019
parent reply Den_d_y <tod.naz ya.ru> writes:
On Tuesday, 25 June 2019 at 16:44:28 UTC, H. S. Teoh wrote:
 On Tue, Jun 25, 2019 at 12:08:07PM +0000, Den_d_y via 
 Digitalmars-d-learn wrote:
 Hello! Here I am again, with my problem ... In my program, I 
 cannot manage to convert from "double" to "int". Here is the 
 code:
[...] Did you try this? import std.conv : to; double d = ...; int i = d.to!int; T
This does not work. The program hangs at this stage, even the code you have proposed also does not work as we would like. Maybe I'm doing something wrong?
Jun 25 2019
parent reply Den_d_y <tod.naz ya.ru> writes:
On Wednesday, 26 June 2019 at 05:53:29 UTC, Den_d_y wrote:
 On Tuesday, 25 June 2019 at 16:44:28 UTC, H. S. Teoh wrote:
 On Tue, Jun 25, 2019 at 12:08:07PM +0000, Den_d_y via 
 Digitalmars-d-learn wrote:
 Hello! Here I am again, with my problem ... In my program, I 
 cannot manage to convert from "double" to "int". Here is the 
 code:
[...] Did you try this? import std.conv : to; double d = ...; int i = d.to!int; T
This does not work. The program hangs at this stage, even the code you have proposed also does not work as we would like. Maybe I'm doing something wrong?
Something is tracked: std.conv.ConvOverflowException C: \ D \ dmd2 \ windows \ bin \ .. \ .. \ src \ phobos \ std \ conv.d (1457): Conversion Underflow Overflow What could it be
Jun 26 2019
parent Den_d_y <tod.naz ya.ru> writes:
On Wednesday, 26 June 2019 at 10:49:50 UTC, Den_d_y wrote:
 On Wednesday, 26 June 2019 at 05:53:29 UTC, Den_d_y wrote:
 On Tuesday, 25 June 2019 at 16:44:28 UTC, H. S. Teoh wrote:
 On Tue, Jun 25, 2019 at 12:08:07PM +0000, Den_d_y via 
 Digitalmars-d-learn wrote:
 [...]
[...] Did you try this? import std.conv : to; double d = ...; int i = d.to!int; T
This does not work. The program hangs at this stage, even the code you have proposed also does not work as we would like. Maybe I'm doing something wrong?
Something is tracked: std.conv.ConvOverflowException C: \ D \ dmd2 \ windows \ bin \ .. \ .. \ src \ phobos \ std \ conv.d (1457): Conversion Underflow Overflow What could it be
How stupid ... You just had to assign the value "0" to the value 0 ... The problem is solved.
Jun 26 2019