numbers

How do I check if input is a number in Python?

和自甴很熟 提交于 2019-11-28 14:16:04
I have a Python script which converts a decimal number into a binary one and this obviously uses their input. I would like to have the script validate that the input is a number and not anything else which will stop the script. I have tried an if/else statement but I don't really know how to go about it. I have tried if decimal.isint(): and if decimal.isalpha(): but they just throw up errors when I enter a string. print("Welcome to the Decimal to Binary converter!") while True: print("Type a decimal number you wish to convert:") decimal = int(input()) if decimal.isint(): binary = bin(decimal)

How to generate a random number from 1 to 100 only once?

大憨熊 提交于 2019-11-28 14:03:37
问题 I need to generate a random number from 1 to 100 in AS3 that will not be generated twice. So I need every number be generated until all the numbers are complete. How can I do that? 回答1: Fill an array with the numbers 1 to 100. Randomly shuffle it (use Fisher-Yates shuffle). Take each number starting from the first array index onwards... 回答2: Fill an Array '_randomNumbers' with the numbers 1-100. Each time you need a number use the following: if (_randomNumbers.length>0) { newRandomNumber =

regex number range 1-17

前提是你 提交于 2019-11-28 13:49:56
Can someone tell me how to match a number between 1-17 with a regex: I tried [1-9]|1[0-7] but it matches 8 in the 18 for example because of the first part. Any ideas? I don't want to have leading zeros. edited: The problem is I'm trying to validate a UK postcode like to start with: KW1-17 (it could be KW1, KW2 up to KW17) ... and this is just the outward part. The postcode may be KW15 1BM or KW151BM so I can't just use anchors to match the whole string ... it becomes more complicated. You need to put anchors around the regex or it will match substrings: ^(?:[2-9]|1[0-7]?)$ I've also taken the

How to ensure javascript addition instead of string concatenation (Not always adding integers)

我是研究僧i 提交于 2019-11-28 13:47:52
Through my javascript library, I end up with a string that represents a number. Now I want to preform an addition on that number without it doing a string concatenation instead. The solution is easy ( How do I add an integer value with javascript (jquery) to a value that's returning a string? , How to make an addition instead of a concatenation ) if your number is always in integer. However my string could be a float or an integer, and at the time of the addition, and I don't know which it will be. Is there a way to make sure the addition happens regardless of whether it's a float or integer?

Datatype to store 20 digit number

瘦欲@ 提交于 2019-11-28 13:38:06
I have a number of 20 digit, which datatype will support to store this number? I have tried long, double but I 'm getting out of range. Number = 48565664968483514466 Then I have to convert this number to Base36 to generate the barcode. BigInteger: The BigInteger class allocates as much memory as it needs to hold all the bits of data it is asked to hold and also provides operations analogues to all of Java's primitive integer operators and for all relevant methods from java.lang.Math. Declare it as BigInteger bi1 = new BigInteger("12345678900123"); To convert your number in base 36: BigInteger

Fixed point vs Floating point number

假如想象 提交于 2019-11-28 13:25:02
问题 I just can't understand fixed point and floating point numbers due to hard to read definitions about them all over Google. But none that I have read provide a simple enough explanation of what they really are. Can I get a plain definition with example? 回答1: A fixed point number has a specific number of bits (or digits) reserved for the integer part (the part to the left of the decimal point) and a specific number of bits reserved for the fractional part (the part to the right of the decimal

Java Double value = 0.01 changes to 0.009999999999999787 [duplicate]

◇◆丶佛笑我妖孽 提交于 2019-11-28 13:24:37
Possible Duplicate: Why not use Double or Float to represent currency? I'm writing a basic command-line program in Java for my high school course. We're only working with variables right now. It's used to calculate the amount of bills and coins of whatever type in your change after a purchase. This is my program: class Assign2c { public static void main(String[] args) { double cost = 10.990; int paid = 20; double change = paid - cost; int five, toonie, loonies, quarter, dime, nickel, penny; five = (int)(change / 5.0); change -= five * 5.0; toonie = (int)(change / 2.0); change -= toonie * 2.0;

Why is not 'decimal.Decimal(1)' an instance of 'numbers.Real'?

二次信任 提交于 2019-11-28 13:23:38
I try to check if a variable is an instance of a number of any type ( int , float , Fraction , Decimal , etc.). I cam accross this question and its answer: How to properly use python's isinstance() to check if a variable is a number? However, I would like to exclude complex numbers such as 1j . The class numbers.Real looked perfect but it returns False for Decimal numbers... from numbers Real from decimal import Decimal print(isinstance(Decimal(1), Real)) # False In contradiction, it works fine with Fraction(1) for example. The documentation describes some operations which should work with the

c/c++ notation of double floating point values

落花浮王杯 提交于 2019-11-28 13:00:30
问题 What's the notation for double precision floating point values in C/C++? .5 is representing a double or a float value? I'm pretty sure 2.0f is parsed as a float and 2.0 as a double but what about .5? http://c.comsci.us/etymology/literals.html 回答1: It's double. Suffix it with f to get float. And here is the link to reference document: http://en.cppreference.com/w/cpp/language/floating_literal 回答2: Technically, initializing a float with a double constant can lead to a different result (i.e.

drawing random number from a standard normal distribution using the standard rand() functions

柔情痞子 提交于 2019-11-28 12:56:34
问题 So most programming languages contain a rand() functionality whereby you can generate a random number between 0 to 1.... My question is, is there a way to manipulate this standard rand() functionality such that you are able to instead draw a random number from normal distribution with a mean and standard deviation? Notice that I'm not asking whether or not a language has a normal distribution functionality built in, I'm asking if you can "simulate" drawing a normal distribution random