numbers

C++ How to output number with at least one number behind the decimal mark

a 夏天 提交于 2019-12-05 07:18:44
How can I make my program output a number with at least one number behind the decimal mark C++? Output: 1 = 1.0 or 1.25 = 1.25 or 2.2 = 2.2 or 3.456789 = 3.456789 Thanks in advance Use showpoint to force the decimal point to be printed double x = 1.0; std::cout << std::showpoint << x << "\n"; It will be followed by the number of 0 required to satisfy the precision of the stream. #include <cmath> #include <iostream> #include <limits> struct FormatFloat { static constexpr const double precision = std::sqrt(std::numeric_limits<double>::epsilon()); const double value; FormatFloat(double value) :

Using Javascript to sort an array of numeric arrays

不想你离开。 提交于 2019-12-05 06:27:32
In Javascript, if I have an array of arrays, like the following: X = [ [1,2,3,4], [1,1,2,3], [1,1,3], [1,4], [2,1,2], [2,2] ] Javascript sorts my array, comparing first entry first, then second, and so on, so that X.sort() returns the following: [ [1,1,2,3], [1,1,3], [1,2,3,4], [1,4], [2,1,2], [2,2] ] Which is what I want. The problem is that the comparison operator for comparing the elements in the arrays is lexicographical, so [10,2] < [2,2] , and, for example, [[10,2],[1,1,3],[2,2]].sort() -> [[1,1,3],[10,2],[2,2]] I need it to sort numerically, so that I get a sorted array of [[1,1,3],[2,2

笑着哭i 提交于 2019-12-05 06:26:33
堆其实就是一棵完全二叉树(若设二叉树的深度为h,除第 h 层外,其它各层 (1~h-1) 的结点数都达到最大个数,第 h 层所有的结点都连续集中在最左边)。 父节点总是大于或等于子节点,这种情况下被叫作 大顶堆 ,或者父节点总是小于或等于子节点,这种情况下叫作 小顶堆 。注意,给定父节点的子节点不一定按顺序排列。 堆不是容器,而是一种特殊的数据组织方式。 STL中堆的实现 #include <iostream> #include <vector> #include <algorithm> #include <string> #include <time.h> using namespace std; int main() { std::vector<double>numbers{ 2.5,10.0,3.5,6.5,8.0,12.0,1.5,6.0 }; std::make_heap(std::begin(numbers), std::end(numbers)); //默认使用<运算符,生成大顶堆,Result: 12 10 3.5 6.5 8 2.5 1.5 6 /*添加元素*/ numbers.push_back(11); // Result: 12 10 3.5 6.5 8 2.5 1.5 6 11 std::push_heap(std::begin(numbers), std

How to represent a number in base 2^32?

烈酒焚心 提交于 2019-12-05 05:53:40
If I have some base 10 or base 16 number, how do I change it into base 2^32? The reason I'm trying to do this, is for implementing BigInt as suggested by other members here .. Why to use higher base for implementing BigInt? Will it be the same as integer(base 10) till 2^32? What will happen after it? You are trying to find something of the form a0 + a1 * (2^32) + a2 * (2^32)^2 + a3 * (2^32)^3 + ... which is exactly the definition of a base-2 32 system, so ignore all the people that told you that your question doesn't make sense! Anyway, what you are describing is known as base conversion .

R - how to re-order data frame by row index number

岁酱吖の 提交于 2019-12-05 05:52:46
This may be a very basic question but I could't find it. Let's say I have a data frame d with row numbers in disorder like this: Signal 4 9998 3 549 1 18 5 2.342 2 0.043 How can I sort this by increasing row index numbers to obtain the following? Signal 1 18 2 0.043 3 549 4 9998 5 2.342 Paulo E. Cardoso d <- read.table(text=readClipboard(), header=TRUE) d$index <- as.numeric(row.names(d)) d[order(d$index), ] you can also use this : d[order(as.numeric(rownames(d))),,drop=FALSE] drop is useful only if your data.frame has one column otherwise remove it Yohanes Lie rownames(d) <- 1 : length

Show 1k instead of 1,000

送分小仙女□ 提交于 2019-12-05 05:47:54
function restyle_text($input){ $input = number_format($input); $input_count = substr_count($input, ','); if($input_count != '0'){ if($input_count == '1'){ return substr($input, +4).'k'; } else if($input_count == '2'){ return substr($input, +8).'mil'; } else if($input_count == '3'){ return substr($input, +12).'bil'; } else { return; } } else { return $input; } } This is the code I have, I thought it was working. apparently not.. can someone help since I can't figure this out. Try this: http://codepad.viper-7.com/jfa3uK function restyle_text($input){ $input = number_format($input); $input_count

C++ Bessel function for complex numbers

喜夏-厌秋 提交于 2019-12-05 05:02:27
问题 I want to implement the Bessel functions of first and second kindDescription of bessel functions for complex numbers in C++. Now I am looking for possibilities to introduce them in my source code. Since math.h only contains bessel functions for real numbers, I would be interested in seeing any kind of possibility. 回答1: The Boost library implements ordinary Bessel functions of the first and second kind and modified Bessel functions of the first and second kind for both real and complex numbers

separate numbers by comma with asp.net mvc

霸气de小男生 提交于 2019-12-05 03:27:55
I am working on an MVC2 appication. I use data annotations to validate data (both client side and server side). I have several fields in my model that only allows decimal values. As soon as a user types a decimal values I want it to be converted to comma seperated more readable format. For instance 1200 should be formatted to 1,200 while 500 should stay as it is. Here is my model: public virtual GrossFee? Fee { get; set; } And here is how it is on the view: %: Html.TextBoxFor(model => model.GrossFee)%> Any ideas regarding this will be highly appreciated. Thanks! Instead of the Html.TextBoxFor

Java and C# - byte array to long conversion difference

好久不见. 提交于 2019-12-05 03:25:43
This is strange to me: when I run in Java byte[] data = new byte[] { 50, -106, 40, -22, -94, -119, -52, 8 }; ByteBuffer bb = ByteBuffer.wrap( data ); System.out.println( bb.getLong() ); result is 3645145936617393160 when I run in C# //unsigned values (signed&0xff) byte[] bytes = new byte[] { 50, 150, 40, 234, 162, 137, 204, 8 }; long l = BitConverter.ToInt64(bytes, 0); System.Console.Write(String.Format("{0}\n", l)); System.Console.ReadKey(); result is 634032980358633010 Can you help me to understand this? Thanks! This is a difference in endianness . If you reverse the byte array, it works as

Need help generating discrete random numbers from distribution

折月煮酒 提交于 2019-12-05 02:43:24
I searched the site but did not find exactly what I was looking for... I wanted to generate a discrete random number from normal distribution. For example, if I have a range from a minimum of 4 and a maximum of 10 and an average of 7. What code or function call ( Objective C preferred ) would I need to return a number in that range. Naturally, due to normal distribution more numbers returned would center round the average of 7. As a second example, can the bell curve/distribution be skewed toward one end of the other? Lets say I need to generate a random number with a range of minimum of 4 and