473,699 Members | 2,570 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to convert a negative value to positive

Hello

Sorry this really is a newbie question!

I am doing some floating point arithmetic and calculating the time
difference between two dates. The date values being comparied are actually
double values. I was going to compare the dates by calculating the
difference betwen the two numbers. But sometimes you get a -ve and
sometimes a +ve value. I want to disregard a small variance eg of up to two
minutes but what is best way to compare values.

ie calculate difference and then convert any -ve values to +ve then check if
(diff > x)

or some other way?

AC
Nov 14 '05 #1
2 11259
>Sorry this really is a newbie question!

I am doing some floating point arithmetic and calculating the time
difference between two dates. The date values being comparied are actually
double values. I was going to compare the dates by calculating the
difference betwen the two numbers. But sometimes you get a -ve and
sometimes a +ve value.
The unary minus operator will convert a negative value to a positive one.
I want to disregard a small variance eg of up to two
minutes but what is best way to compare values.

ie calculate difference and then convert any -ve values to +ve then check if
(diff > x)


You may want to use fabs(), in code similar to this:
#include <math.h>
double x1;
double x2;
double delta;

... put values in x1, x2, and delta ...;
if (fabs(x1-x2) > delta) { ... too far apart ... ; }
else { ... pretty close ... ; }

Gordon L. Burditt
Nov 14 '05 #2
Angus Comber wrote:
I am doing some floating point arithmetic and calculating the time
difference between two dates. The date values being comparied are actually
double values. I was going to compare the dates by calculating the
difference betwen the two numbers. But sometimes you get a -ve and
sometimes a +ve value. I want to disregard a small variance eg of up to two
minutes but what is best way to compare values.


Personally, storing dates as floating point seems
very strange to me. Yes, time is continuous(*), but not the
way it is usually measured, and not the way you want it
measured.

(*) I believe it is still unknown if quantum mechanics quantizes
time or not.

-- glen

Nov 14 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2652
by: David Levine | last post by:
Using v1.1. of the framework: I was writing a wrapper around the java.util.zip.ZipOutputStream class so that my app could access it using standard FileStream semantics and I ran into a conversion problem. The signature for the write method is void ZipOutputStream.Write(sbyte array,int off,int len) so I figured a simple conversion from byte to sbyte would do the trick. Now, my understanding of an sbyte is that it is a signed byte that...
11
6291
by: Grant Edwards | last post by:
I give up, how do I make this not fail under 2.4? fcntl.ioctl(self.dev.fileno(),0xc0047a80,struct.pack("HBB",0x1c,0x00,0x00)) I get an OverflowError: long int too large to convert to int ioctl() is expecting a 32-bit integer value, and 0xc0047a80 has the high-order bit set. I'm assuming Python thinks it's a signed value. How do I tell Python that 0xc0047a80 is an unsigned 32-bit value?
2
32316
by: Goran | last post by:
Hi! I need to convert from a unsigned char array to a float. I don't think i get the right results in the program below. unsigned char array1 = { 0xde, 0xc2, 0x44, 0x23}; //I'm not sure in what order the data is stored so i try both ways. unsigned char array2 = { 0x23, 0x44, 0xc2, 0xde}; float *pfloat1, *pfloat2;
7
13080
by: Golan | last post by:
Hi, I need to convert a Binary value to Decimal. I've been told that the value is an unsigned one. How can I do this? I use memcpy into an unsigned char variable, but when I print the value I got a negative value. For example if I'm using the xd -c (Unix) on the file, I can see the value FFFFFFFFFFFFFFA2 which using the memcpy as described above I get -94. But the real value that I'd expect to get is a positive one.
2
3542
by: [Gho] | last post by:
How to convert a Hexa value to negative and non negative value : if i get 0x1d = result should be 29 but if i get fd = result should be -3 How to do that
15
35496
by: jaks.maths | last post by:
How to convert negative integer to hexadecimal or octal number? Ex: -568 What is the equivalent hexadecimal and octal number??
39
4007
by: Frederick Gotham | last post by:
I have a general idea about how negative number systems work, but I'd appreciate some clarification if anyone would be willing to help me. Let's assume we're working with an 8-Bit signed integer, and that it contains no padding. Firstly, I realise that the MSB is known as the sign-bit, and that it indicates whether the number is positive or negative (irrespective of which negative number system is used).
30
17316
by: ceeques | last post by:
Hi I am a novice in C. Could you guys help me solve this problem - I need to convert integer(and /short) to string without using sprintf (I dont have standard libray stdio.h). for instance:- int i =2; char ch 'A'
19
3601
by: VK | last post by:
http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/ b495b4898808fde0> is more than one month old - this may pose problem for posting over some news servers. This is why I'm starting a new one] I'd still like to finish this rounding mess. As a startup lemma we can take that VK is the worst programmer of all times and places: let's move from here forward please. The usability of any program depends on exact behavior...
0
8689
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8618
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9178
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9035
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8885
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7752
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5875
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4376
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2348
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.