digitalmars.D.learn - D do not know which function to use apparently
- Narxa (15/15) Dec 11 2018 Hello, people!
- Adam D. Ruppe (4/5) Dec 11 2018 You can just cast it to float:
- Narxa (6/11) Dec 11 2018 It produced the following error:
- Steven Schveighoffer (3/20) Dec 11 2018 You need to cast the result back to int.
- Narxa (7/28) Dec 11 2018 Yes, it worked:
- Andrea Fontana (8/13) Dec 11 2018 I think you don't need floor.
Hello, people! The following case: ------------------- int var = floor(sqrt(n)) // where 'n' is a 'const int' Produces the following error: ----------------------------- Error: std.math.sqrt called with argument types (const(int)) matches both: /usr/include/dmd/phobos/std/math.d(2067): std.math.sqrt(float x) and: /usr/include/dmd/phobos/std/math.d(2073): std.math.sqrt(real x) Please, could you tell me how do I solve this?! Thank you!
Dec 11 2018
On Tuesday, 11 December 2018 at 14:16:05 UTC, Narxa wrote:int var = floor(sqrt(n)) // where 'n' is a 'const int'You can just cast it to float: floor(sqrt( cast(float) n )); and it will work.
Dec 11 2018
On Tuesday, 11 December 2018 at 14:22:30 UTC, Adam D. Ruppe wrote:On Tuesday, 11 December 2018 at 14:16:05 UTC, Narxa wrote:It produced the following error: -------------------------------- Error: cannot implicitly convert expression floor(sqrt(cast(float)n)) of type float to int I am using 'dmd' compiler if it matters.int var = floor(sqrt(n)) // where 'n' is a 'const int'You can just cast it to float: floor(sqrt( cast(float) n )); and it will work.
Dec 11 2018
On 12/11/18 9:47 AM, Narxa wrote:On Tuesday, 11 December 2018 at 14:22:30 UTC, Adam D. Ruppe wrote:You need to cast the result back to int. -SteveOn Tuesday, 11 December 2018 at 14:16:05 UTC, Narxa wrote:It produced the following error: -------------------------------- Error: cannot implicitly convert expression floor(sqrt(cast(float)n)) of type float to int I am using 'dmd' compiler if it matters.int var = floor(sqrt(n)) // where 'n' is a 'const int'You can just cast it to float: floor(sqrt( cast(float) n )); and it will work.
Dec 11 2018
On Tuesday, 11 December 2018 at 14:53:02 UTC, Steven Schveighoffer wrote:On 12/11/18 9:47 AM, Narxa wrote:Yes, it worked: --------------- int var = cast(int) floor(sqrt( cast(float) n )); I thought floor already returned an 'int' but I was mistaken. Thank you very much.On Tuesday, 11 December 2018 at 14:22:30 UTC, Adam D. Ruppe wrote:You need to cast the result back to int. -SteveOn Tuesday, 11 December 2018 at 14:16:05 UTC, Narxa wrote:It produced the following error: -------------------------------- Error: cannot implicitly convert expression floor(sqrt(cast(float)n)) of type float to int I am using 'dmd' compiler if it matters.int var = floor(sqrt(n)) // where 'n' is a 'const int'You can just cast it to float: floor(sqrt( cast(float) n )); and it will work.
Dec 11 2018
On Tuesday, 11 December 2018 at 15:17:10 UTC, Narxa wrote:Yes, it worked: --------------- int var = cast(int) floor(sqrt( cast(float) n )); I thought floor already returned an 'int' but I was mistaken. Thank you very much.I think you don't need floor. int var = cast(int)(sqrt(cast(float)n)); or: int var = cast(int)(float(i).sqrt); or: int var = float(i).sqrt.to!int; Andrea
Dec 11 2018