casting

How to compute the digits of an irrational number one by one?

£可爱£侵袭症+ 提交于 2020-12-06 16:56:43
问题 I want to read digit by digit the decimals of the sqrt of 5 in C. The square root of 5 is 2,23606797749979..., so this'd be the expected output: 2 3 6 0 6 7 9 7 7 ... I've found the following code: #include<stdio.h> void main() { int number; float temp, sqrt; printf("Provide the number: \n"); scanf("%d", &number); // store the half of the given number e.g from 256 => 128 sqrt = number / 2; temp = 0; // Iterate until sqrt is different of temp, that is updated on the loop while(sqrt != temp){ /

How to compute the digits of an irrational number one by one?

家住魔仙堡 提交于 2020-12-06 16:55:23
问题 I want to read digit by digit the decimals of the sqrt of 5 in C. The square root of 5 is 2,23606797749979..., so this'd be the expected output: 2 3 6 0 6 7 9 7 7 ... I've found the following code: #include<stdio.h> void main() { int number; float temp, sqrt; printf("Provide the number: \n"); scanf("%d", &number); // store the half of the given number e.g from 256 => 128 sqrt = number / 2; temp = 0; // Iterate until sqrt is different of temp, that is updated on the loop while(sqrt != temp){ /

How to perform a checked cast?

若如初见. 提交于 2020-12-02 08:21:50
问题 I am new to Generics and am having an issue. Consider the following code: public class A {} public class B extends A {} public <T extends A> T getB() { A test = new B(); Class<B> clazz = B.class; if (clazz.isInstance(test)) { return (T)test; } return null; } This generates an Unchecked cast warning. on the return (T)test; line. but clearly I am checking the type with the if (clazz.isInstance(test)) line. Is there a way to do a "checked cast"? I'm not looking to just suppress the warning but

How do I idiomatically convert a bool to an Option or Result in Rust?

一曲冷凌霜 提交于 2020-11-29 04:21:03
问题 It seems there is no way of such one-line conversion using std . I do not like this kind of verbosity: match my_bool { true => Ok(()), false => Err(MyError::False), } I would like to use some kind of one-liner, e.g.: let my_bool = true; let my_option = my_bool.to_option(MyObject{}); // true => MyObject{}, false => None let my_result = my_bool.to_result(MyObject{}, MyError{}); // true => MyObject{}, false => MyError{} What is the shortest piece of code doing that? 回答1: There is the boolinator

How do I idiomatically convert a bool to an Option or Result in Rust?

不羁岁月 提交于 2020-11-29 04:19:26
问题 It seems there is no way of such one-line conversion using std . I do not like this kind of verbosity: match my_bool { true => Ok(()), false => Err(MyError::False), } I would like to use some kind of one-liner, e.g.: let my_bool = true; let my_option = my_bool.to_option(MyObject{}); // true => MyObject{}, false => None let my_result = my_bool.to_result(MyObject{}, MyError{}); // true => MyObject{}, false => MyError{} What is the shortest piece of code doing that? 回答1: There is the boolinator

Typescript: How to convert a string to a type

限于喜欢 提交于 2020-11-24 13:35:17
问题 I am creating a bunch of web components and each of them can emit custom events. As an example here are two simple examples: //MyButton emits 'myButtonPressed' and detail of this type: interface ButtonDetailType { id: string; } //MySlider emits 'mySliderChanging' and details of this type: interface SliderChangingDetailType { id: string; value: number; } emits 'mySliderChanged' and details of this type: interface SliderChangedDetailType { id: string; oldValue: number; newValue: number; } To

Typescript: How to convert a string to a type

☆樱花仙子☆ 提交于 2020-11-24 13:33:36
问题 I am creating a bunch of web components and each of them can emit custom events. As an example here are two simple examples: //MyButton emits 'myButtonPressed' and detail of this type: interface ButtonDetailType { id: string; } //MySlider emits 'mySliderChanging' and details of this type: interface SliderChangingDetailType { id: string; value: number; } emits 'mySliderChanged' and details of this type: interface SliderChangedDetailType { id: string; oldValue: number; newValue: number; } To

Typescript: How to convert a string to a type

北战南征 提交于 2020-11-24 13:31:28
问题 I am creating a bunch of web components and each of them can emit custom events. As an example here are two simple examples: //MyButton emits 'myButtonPressed' and detail of this type: interface ButtonDetailType { id: string; } //MySlider emits 'mySliderChanging' and details of this type: interface SliderChangingDetailType { id: string; value: number; } emits 'mySliderChanged' and details of this type: interface SliderChangedDetailType { id: string; oldValue: number; newValue: number; } To